Git Product home page Git Product logo

tensorflow-vgg's Introduction

Tensorflow VGG16 and VGG19

This is a Tensorflow implemention of VGG 16 and VGG 19 based on tensorflow-vgg16 and Caffe to Tensorflow. Original Caffe implementation can be found in here and here.

We have modified the implementation of tensorflow-vgg16 to use numpy loading instead of default tensorflow model loading in order to speed up the initialisation and reduce the overall memory usage. This implementation enable further modify the network, e.g. remove the FC layers, or increase the batch size.

To use the VGG networks, the npy files for VGG16 NPY or VGG19 NPY has to be downloaded.

Usage

Use this to build the VGG object

vgg = vgg19.Vgg19()
vgg.build(images)

or

vgg = vgg16.Vgg16()
vgg.build(images)

The images is a tensor with shape [None, 224, 224, 3].

Trick: the tensor can be a placeholder, a variable or even a constant.

All the VGG layers (tensors) can then be accessed using the vgg object. For example, vgg.conv1_1, vgg.conv1_2, vgg.pool5, vgg.prob, ...

test_vgg16.py and test_vgg19.py contain the sample usage.

Extra

This library has been used in my another Tensorflow image style synethesis project: stylenet

Update 1: Trainable VGG:

Added a trainable version of the VGG19 vgg19_trainable. It support train from existing vaiables or from scratch. (But the trainer is not included)

A very simple testing is added test_vgg19_trainable, switch has demo about how to train, switch off train mode for verification, and how to save.

A seperated file is added (instead of changing existing one) because I want to keep the simplicity of the original VGG networks.

Update 2: Tensorflow v1.0.0:

All the source code has been upgraded to v1.0.0.

The conversion is done by my another project tf0to1

tensorflow-vgg's People

Contributors

a514514772 avatar amylizzle avatar insikk avatar jamoque avatar machrisaa avatar sankichi92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tensorflow-vgg's Issues

Fine-tune the model

Hi, I'm trying to use vgg19 on my problem which is a two-class classification, so I want to fine-tune the model on my dataset and I have modified the 'fc8' layer in ''vgg19_trainable.py''(in-size:4096, out-size:2).
I want to set the batch-size to be 32, so should I first read and reshape the 32 images and use 'np.concatenate()' to combine the images, just like the code in the 'test_vgg10.py'? And I think the label-list for each batch is a list type which contains 32 1-hot label(each label is also a list type), am I doing right?

What about use this pre-trained model to train new stuffs?

What should I do if I want to add more class to that pre-trained weight file? So now the class should goes to 1001. So that it can recognize those 1000 objects have already been trained and also can recognize the thing I added in. Will that be possible?

Thanks.

Error when running test_vgg19.py

I've been trying to run your test_vgg19.py, but get the following error:

ValueError: GraphDef cannot be larger than 2GB.

It seems like the graph is getting too big. So I'm wondering whether I'm doing something really stupid or whether you have the same problem?

Organize the project as Python package

It would be really nice if the repo/project would be organized as an ordinary Python package so it could be installed and used globally more easily. Currently this is a bit difficult, especially when you work in environments when it's not so easy to setup custom modules likes it's often applies in ML.

Update: check my fork which I have organized as package. If you satisfied with that, I could open a pull request.

AssertionError

When I try thetransf_learning.py,some wrong happens:
Traceback (most recent call last):
File "transfer_learning.py", line 19, in
vgg.build(input_)
File "F:\VGG16-1\tensorflow_vgg\vgg16.py", line 36, in build
assert red.get_shape().as_list()[1:] == [224, 224, 1]
AssertionError.
Could you give me some advice?

code for training the net

Great work. Do you have code that train the vgg net and save the model weights to the npy file ?

Thanks,

What database did this vgg implementation trained on?

Hi,

I'm using this library to do some research, and it will be a great help if I can know which database was the model trained on. If it is Imagenet, what's the version of the data from Imagenet, is it ILSVRC2012 or ILSVRC2014?
Thanks.

Wanquan,

preprocessing image for VGG16, BGR or RGB ?

VGG16 were trained using Caffe, and Caffe uses OpenCV to load images which uses BGR by default, so VGG models are expecting BGR images. In the example, RGB images are used, but I'm not sure whether is it right because I couldn't find the op of swapping the first-layer filters of the CNN when convert caffe model to tensorflow model.

ImportError: undefined symbol: PyClass_Type

when I try to load module vgg19.py,it comes to this error:
import tensorflowvgg.vgg19

Traceback (most recent call last):

File "", line 1, in
import tensorflowvgg.vgg19

File "/home/yan/下载/tensorflowvgg/vgg19.py", line 2, in
import tensorflow as tf

File "/usr/local/lib/python2.7/dist-packages/tensorflow/init.py", line 23, in
from tensorflow.python import *

File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/init.py", line 45, in
from tensorflow.python import pywrap_tensorflow

File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in
_pywrap_tensorflow = swig_import_helper()

File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)

File "/home/yan/anaconda3/lib/python3.6/imp.py", line 242, in load_module
return load_dynamic(name, filename, file)

File "/home/yan/anaconda3/lib/python3.6/imp.py", line 342, in load_dynamic
return _load(spec)

ImportError: /usr/local/lib/python2.7/dist-packages/tensorflow/python/_pywrap_tensorflow.so: undefined symbol: PyClass_Type

my python version is: Python 3.6.1 |Anaconda custom (64-bit)| (default, May 11 2017, 13:09:58)

Memory Usage

I am looking to fine tune the VGG19 model for an image classification problem, and am running into memory issues on my GPU.

I was wondering how much memory is expected to be used when training this network? When running it on a K80 with a 11 gb available, it was using 6.4 gb while training on just the two demo images.

I have heard of people running vgg19 on graphics cards with less than 4 gb of memory, so I was wondering if there room for improvement reducing the memory usage of this implementation?

VGG Numpy files distribution.

Hi Chris,
I've used your TF implementation of VGGnet in my project and linked your dropbox storage to download the numpy file. However, it looks like the link was disabled due to too much traffic. Would it be alright if I reuploaded it somewhere and linked there?

Thanks,
Jan

Wondering about data input format

Hi! My group and I are trying to modify this model to accomplish a model proposed in the paper "Deep3D"
We are just wondering if using the ndarray input is required. Tensorflow has a decode_jpeg method. Would that work with these pre trained weights? we are training on around 1 million 3D movie frames and it would save a lot of memory scaling problems if jpegs are used instead.

Please let us know!

Thanks

Order of image channels?

Hi, I wonder which order is used in your implementation of VGG19? As far as I know, BGR is used for Caffe, and since the trained model you provide here is adapted from Caffe's model, so by default BGR is also used here?

Your trainable VGG 19 finetune issue

@machrisaa I have two questions from you in your Vgg19 trainable implementation.
I am using your code and your pretrianed, but, now if want to finetune this network for some other problem using triplet loss, then, how could i implement back propogation? Is that done automatically using your code.
I have written a code, if you have time i can send you my code to view.

KeyError Message after loading the npy file

Hi, Thank you for sharing the code.

I have been trying to running the test_vgg16.py, and it gives the error message as follows. Would you like to let me know the possible reason for this?

npy file loaded build model started Traceback (most recent call last): File "test_vgg16.py", line 23, in <module> vgg.build(images) File "/home/DL-Phase3/tensorflow-vgg-master/vgg16.py", line 47, in build self.conv1_1 = self.conv_layer(bgr, "conv1_1") File "/home/DL-Phase3/tensorflow-vgg-master/vgg16.py", line 92, in conv_layer filt = self.get_conv_filter(name) File "/home/DL-Phase3/tensorflow-vgg-master/vgg16.py", line 120, in get_conv_filter return tf.constant(self.data_dict[name][0], name="filter") KeyError: 0

tensorflow version and KeyError

Thanks for your work first. I wonder which version of tensorflow did you use in this project? My tf version is 0.10.0 and when I run your code I got such an error:

Traceback (most recent call last):
File "test_vgg16.py", line 22, in <module>
   vgg.build(images)
File "/Users/yobichi/bigdata/gln/tensorflow-vgg-master/vgg16.py", line 46, in build
   self.conv1_1 = self.conv_layer(bgr, "conv1_1")
File "/Users/yobichi/bigdata/gln/tensorflow-vgg-master/vgg16.py", line 91, in conv_layer
   filt = self.get_conv_filter(name)
 File "/Users/yobichi/bigdata/gln/tensorflow-vgg-master/vgg16.py", line 120, in get_conv_filter
   return tf.constant(self.data_dict[name][0], name="filter")
 KeyError: 0

Do you have any idea? Thanks in advance!

Odd classification results

Hi,

i just cloned your rep and downloaded the *.npy files provided. I didn't change any file and run the test_vgg16.py example, which works but leads to odd classification results:

[...]
npy file loaded
[[  3.54046060e-05   6.26692083e-03   2.32575258e-04 ...,   1.31252409e-05
    4.17124102e-04   5.32930251e-03]
 [  2.74504941e-07   1.49452433e-04   5.41723421e-06 ...,   1.50292351e-06
    1.41892961e-04   7.05321319e-03]
 [  2.73421301e-05   2.04334548e-03   5.73722122e-04 ...,   1.37338357e-05
    5.19106514e-04   4.67342697e-03]]
('prob shape', (1000,))
('Top1: ', 'n04589890 window screen', 0.17731868)
('Top5: ', [('n04589890 window screen', 0.17731868), ('n03788365 mosquito net', 0.050591175), ('n03000134 chainlink fence', 0.038932227), ('n04476259 tray', 0.036342748), ('n03485794 handkerchief, hankie, hanky, hankey', 0.033405658)])
('prob shape', (1000,))
('Top1: ', 'n04209239 shower curtain', 0.3111819)
('Top5: ', [('n04209239 shower curtain', 0.3111819), ('n02971356 carton', 0.061327737), ('n04548280 wall clock', 0.036779501), ('n03131574 crib, cot', 0.032009162), ('n03291819 envelope', 0.031746123)])

Any thoughts on what might be the problem? I'm using tensorflow 0.8.0 and tensorflow-vgg16 actually works fine.

Thanks!

Accuracy of the trained model file.

Is the model file VGG19.NPY trained on imagenet? How about the accuracy?

I am sorry to open this issue in a hurry. I just found that I should convert image to BGR.

python3 compability: load() got an unexpected keyword argument 'encoding'

I got the following error when I run it on python3:

  File "/home/kang/Documents/work_code_PC1/tensorflow-vgg/vgg16.py", line 20, in __init__
    self.data_dict = np.load(vgg16_npy_path, encoding='latin1').item()

TypeError: load() got an unexpected keyword argument 'encoding'

How to fix it?
Thank you very much.

Training from scratch

Hello, i am tf beginner. When i look at your code, i didnt see any instruction of how to training vgg from scratch or fine tuning from specified weights. Would you mind showing me how to do with your code ? Thanks a lot

Training does not work

I use test_vgg19_trainable to train the vgg19 net.
I feed a photo of a face (not tiger), telling the net it is a tiger (#292=1).
But in every run the system says it is a "cup" (#968) with p=1.

Style transfer without providing segmentation masks

Is it possible to use the code without providing segmentation masks?
I tried this but it didn't work:

python deep_photostyle.py --content_image_path ./examples/input/in11.png - -style_image_path ./examples/style/tar11.png --style_option 1

Wget links for VGG*.npy.

It's a pain to create an account etc to just download that file. Also a wget link will be better since I am guessing folks are downloading this to a server instead of to local machine.

How I do fine-tuning using VGG19 on my Own Data in TF

For example, I want to classify 25 labels of my image data, which means there are 25 outputs. I don't know how to ignore the last FC-layer parameters. My idea is first to load all pre-trained parameters and then change the network in memory(change the last fully connected layer, randomly initialize it), then feed my data in and add the optimizer, re-train the network? I don't if it is sensible. Hope for help!!!

download vgg16.npy

你好,我发现vgg16.npy的下载链接打不开,是链接过期了吗?

Regarding training the vgg model

Hi Chris,

If we need to re-train the vgg model, how to modify your current code to the training process? Or are there any tensorflow framework that can incorporate your code into a training process?

Thanks,

wenouyang

How to use it to train my own data

I am having a program to classify pollen in 2 types: sugi and hinoki. The teaching data are 100x100 images of only 1 pollen, and the real data are bigger images with lots of pollen inside (which I used opencv to mark circle in each pollen, with suitable color according to its type).

Teacher asked me to change the program into a new version using vgg (16 or 19), but I really dont know how. I have looked up in the internet, and most of versions use image_net weight, not trainable one. There is only this version seem to be useful but I dont know how to change it to fit my program . I am really new to neuron network and tensorflow, so please help me.

Here is my program.

``# coding: UTF-8
#opencv bgr
#rgb
import numpy as np
import tensorflow as tf
import os
import sys
import csv
import random
from datetime import datetime
from PIL import Image
from matplotlib import pylab as plt
from time import sleep
import cv2
#import cv2.cv as cv

DataShape = (100,100,3)
Ratio = 1#2000.0
TestNum = 100

def show_img(dataDir):

    data = np.load(dataDir)#.astype(float)
    #label = int(row[LabelIdx])
    #data += np.random.normal(0,0.1*np.max(data),(512,512))
    #data -= np.min(data)
    #data = data*255/np.max(data)
    #print name
    #print data.shape
    #if name=="os10-020_cancer_full.npy":
    for i in range(data.shape[0]):
        print (i)
        #lt.imshow(data[i])
        #plt.show()
    #plt.imshow(data[data.shape[0]-ZIdx])
    #plt.show()
    #pilImg = Image.fromarray(np.uint8(data[15]))
    #pilImg.show()
    #sleep(1)
    
def show_npy(dataList):

    #data = np.load(dataDir)#.astype(float)
    #label = int(row[LabelIdx])
    #data += np.random.normal(0,0.1*np.max(data),(512,512))
    #data -= np.min(data)
    #data = data*255/np.max(data)
    #print name
    #print data.shape
    #if name=="os10-020_cancer_full.npy":
    for i in range(len(dataList[0])):
        #print i
        #plt.imshow(dataList[2][i])
        #plt.show()
        pilImg = Image.fromarray(np.uint8(dataList[2][i]*255.0))
        pilImg.show()
        sleep(1)
    #plt.imshow(data[data.shape[0]-ZIdx])
    #plt.show()
    #pilImg = Image.fromarray(np.uint8(data[15]))
    #pilImg.show()
    #sleep(1)
 
# nameList  : list of each data name
# labelList : list of teaching data (written by 0 or 1 and 3 calams)
# dataList  : 4 dimensionary data
def GetData():
    sugi_data = np.load('sugi.npy')/255.0
    hinoki_data = np.load('hinoki.npy')/255.0
    nameList = []
    labelList = []
    dataList = []
    print (sugi_data.shape)
    print (hinoki_data.shape)
    #sys.exit()
    
    repeat = np.min((len(sugi_data),len(hinoki_data)))%100
    for i_repeat in range(repeat):
        for i,data in enumerate(sugi_data[i_repeat*100:(i_repeat+1)*100]):
            name = 'sugi_'+str(i)
            data = np.reshape(data,[DataShape[0],DataShape[1],DataShape[2]])
            label = 0
            l = np.zeros(2)
            l[label] += 1

            labelList.append(l)
            nameList.append([name])
            dataList.append(data)

        print (len(sugi_data))

        for i,data in enumerate(hinoki_data[i_repeat*100:(i_repeat+1)*100]):
            name = 'hinoki_'+str(i)
            data = np.reshape(data,[DataShape[0],DataShape[1],DataShape[2]])
            label = 1
            l = np.zeros(2)
            l[label] += 1

            labelList.append(l)
            nameList.append([name])
            dataList.append(data)
        #print len(hinoki_data)
        #print np.array(dataList).shape
        #print np.max(dataList)
    
    #data_shuffle = zip(nameList,labelList,dataList)
    #random.shuffle(data_shuffle)
    #return zip(*data_shuffle)

    return nameList,labelList,dataList

# dataList : 4 dimensionary data
# testIdxList : 1=test data , 0 = training data
def GetDataset(dataList,testIdxList):
    teDataList = []
    trDataList = []
    for i in range(len(dataList)):
        if i in testIdxList:
            teDataList.append(dataList[i])
        else:
            trDataList.append(dataList[i])
    return np.vstack([teDataList]),np.vstack([trDataList])


def Train(alldata,testIdxList,resultName):
    print ("start train initialize")
    os.mkdir("output/" + resultName)
    print ("get dataset")
    ten,trn = GetDataset(alldata[0], testIdxList)
    tel,trl = GetDataset(alldata[1], testIdxList)
    ted,trd = GetDataset(alldata[2], testIdxList)
    #tef,trf = GetDataset(alldata[3], testIdxList)

    #print trd.shape
    #sys.exit()


    #mizumasi 
    #data_num = len(trd)
    #nlist = []
    #llist = []
    #dlist = []
    #flist = []
    #nlist.extend(trn)
    #llist.extend(trl)
    #dlist.extend(trd)
    #flist.extend(trf)
    
    #print data_num
    #label_num = np.zeros(3).astype(np.int32)
    #for i in range(data_num):
    #    for n in range(3):
    #        if trl[i][n]==1 :
    #            label_num[n]+=1
    #print label_num

    #for n in range(3):
    #    for j in range(100-label_num[n]):
    #        flag=1
    #        while(flag==1):
    #          i = random.randint(0,data_num-1)
    #          if trl[i][n]==1:
    #            flag=0  #
    #        nlist.append(trn[i])
    #        llist.append(trl[i])
    #        data = trd[i] + np.random.normal(0,0.1,(DataShape[0],DataShape[1],DataShape[2],1))
    #       dlist.append(data)
            #flist.append(trf[i])

    #for i in range(data_num):
    #    for j in range(10): 
    #        nlist.append(trn[i])
    #        llist.append(trl[i])
    #        data = trd[i] + np.random.normal(0,0.1,(DataShape[0],DataShape[1],DataShape[2],1))
    #        dlist.append(data)
            #flist.append(trf[i])

    #trn = np.array(nlist)
    #trl = np.array(llist)
    #trd = np.array(dlist)
    print ("generate cnn model")
    x = tf.placeholder(tf.float32, [None,DataShape[0],DataShape[1],DataShape[2]])
    y = tf.placeholder(tf.float32, [None,2])

    conv_W0 = tf.Variable(tf.truncated_normal([5,5,3,32],stddev=0.01), name="conv_W0")
    conv_b0 = tf.Variable(tf.zeros([32]), name="conv_b0")
    conv_z0 = tf.nn.conv2d(x, conv_W0, [1,1,1,1], "SAME") + conv_b0
    conv_u0 = tf.nn.relu(conv_z0)
    pool0 = tf.nn.max_pool(conv_u0, [1,2,2,1], [1,2,2,1], "SAME")
    norm0 = pool0#tf.nn.lrn(pool0)   

    conv_W1 = tf.Variable(tf.truncated_normal([5,5,32,64],stddev=0.01), name="conv_W1")
    conv_b1 = tf.Variable(tf.zeros([64]), name="conv_b1")
    conv_z1 = tf.nn.conv2d(norm0, conv_W1, [1,1,1,1], "SAME") + conv_b1
    conv_u1 = tf.nn.relu(conv_z1)
    pool1 = tf.nn.max_pool(conv_u1, [1,2,2,1], [1,2,2,1], "SAME")
    norm1 = pool1#tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta = 0.75, name='norm1')

    reshapeVal = 25*25*64
    flat  = tf.nn.dropout(tf.reshape(norm1,[-1,reshapeVal]),0.5)

    full_W0 = tf.Variable(tf.truncated_normal([reshapeVal,200],stddev=0.01), name="full_W0")
    full_b0 = tf.Variable(tf.zeros([200]), name="full_b0")
    full_z0 = tf.matmul(flat, full_W0)  + full_b0
    full_u0 = tf.nn.relu(full_z0)

    full_W = tf.Variable(tf.truncated_normal([200,2],stddev=0.01), name="full_W")
    full_b = tf.Variable(tf.zeros([2]), name="full_b")
    full_z = tf.matmul(full_u0, full_W)  + full_b
    full_u = tf.nn.softmax(full_z)

    #conv_L2 = tf.nn.l2_loss(conv_W0) + tf.nn.l2_loss(full_W)

    loss = -tf.reduce_mean(y*tf.log(tf.clip_by_value(full_u,1e-10,1.0))) #+ 0.001*conv_L2
    trainstep = tf.train.AdamOptimizer(1e-5).minimize(loss)
    #trainstep = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
    #trainstep = tf.train.FtrlOptimizer(0.0001).minimize(loss)

    collect_prediction = tf.equal(tf.argmax(full_u, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(collect_prediction,tf.float32))
    
    print ("server define")
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    #saver.restore(sess, "/home/miyake/Desktop/kahun_ohira/finalmodel.ckpt")

    f = open("output/" + resultName+"/LearningResult.csv","w")
    writer = csv.writer(f)
    writer.writerow(["loop","train loss","test accuracy"])
    print ("start learning")

    for i in range(1000001):
    #for i in range(101):
        idxList = []
        for j in range(10):
            idxList.append(random.randint(0,trd.shape[0]-1))

        if i % 10 == 0:
            l = sess.run(loss,feed_dict={x:trd[idxList], y:trl[idxList]})
            acc = sess.run(accuracy,feed_dict={x:ted,y:tel})
            writer.writerow([i,l,acc])
            print (str(i) + ": loss = " + str(l) + ", acc = " + str(acc))
            if l<0.0001 :
                saver.save(sess, datetime.now().strftime('%s')+"finalmodel.ckpt")
                break
        if i % 1000 == 0:
            saver.save(sess, "model.ckpt")

        sess.run(trainstep,feed_dict={x:trd[idxList], y:trl[idxList]})

    f.close()
    print ("output result")
    f = open("output/"+resultName+"/TestResult.csv","w")
    writer = csv.writer(f)
    writer.writerow(["Name","IsCollect"])
    isCollect,answer,output,output_data = sess.run((collect_prediction,tf.argmax(y, 1),tf.argmax(full_u, 1),full_u),feed_dict={x:ted,y:tel})
    for i in range(len(output)):
        writer.writerow([ten[i][0],int(isCollect[i]),answer[i],output[i],output_data[i][0],output_data[i][1]])
    tf.reset_default_graph()

def Test(image_path,label,filename,file_path):


    limit_data = []
    limit_label = []
    limit_circles = []
    img = cv2.imread(image_path,cv2.IMREAD_COLOR)
    img_array = np.array(img)
    cimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    cimg = cv2.medianBlur(cimg,5)
    #_,cimg = cv2.threshold(cimg,0,255,cv2.THRESH_BINARY| cv2.THRESH_OTSU)
    #cv2.imwrite(datetime.now().strftime('%s')+"binary.jpg",cimg)
    #sys.exit()

    #circles = cv2.HoughCircles(cimg,cv.CV_HOUGH_GRADIENT,1,10,param1=10,param2=18,minRadius=10,maxRadius=25)
    circles = cv2.HoughCircles(cimg,cv2.HOUGH_GRADIENT,1,20,param1=15,param2=20,minRadius=10,maxRadius=25)
    
    circles = np.uint16(np.around(circles))[0,:]
    print (len(circles))

    for i in circles:
        half = DataShape[0]//2
        zoom_data = img_array[i[1]-half:i[1]+half,i[0]-half:i[0]+half,:]/255.0
        if zoom_data.shape!=DataShape : continue
        limit_data.append(zoom_data)
        l = np.zeros(2)
        l[label] += 1
        limit_label.append(l)
        limit_circles.append(i)
        #cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
        #v2.circle(img,(i[0],i[1]),2,(0,0,255),3)
        #print img_array[i[0]-half:i[0]+half,i[1]-half:i[1]+half,:].shape
    limit_data = np.array(limit_data)
    limit_label = np.array(limit_label)
    limit_circles = np.array(limit_circles)
    label_num = limit_data.shape[0]
    #cv2.imwrite(datetime.now().strftime('%s')+"output.jpg",img)
    #sys.exit()

    x = tf.placeholder(tf.float32, [None,DataShape[0],DataShape[1],DataShape[2]])
    y = tf.placeholder(tf.float32, [None,2])

    conv_W0 = tf.Variable(tf.truncated_normal([5,5,3,32],stddev=0.01), name="conv_W0")
    conv_b0 = tf.Variable(tf.zeros([32]), name="conv_b0")
    conv_z0 = tf.nn.conv2d(x, conv_W0, [1,1,1,1], "SAME") + conv_b0
    conv_u0 = tf.nn.relu(conv_z0)
    pool0 = tf.nn.max_pool(conv_u0, [1,2,2,1], [1,2,2,1], "SAME")
    norm0 = pool0#tf.nn.lrn(pool0)   

    conv_W1 = tf.Variable(tf.truncated_normal([5,5,32,64],stddev=0.01), name="conv_W1")
    conv_b1 = tf.Variable(tf.zeros([64]), name="conv_b1")
    conv_z1 = tf.nn.conv2d(norm0, conv_W1, [1,1,1,1], "SAME") + conv_b1
    conv_u1 = tf.nn.relu(conv_z1)
    pool1 = tf.nn.max_pool(conv_u1, [1,2,2,1], [1,2,2,1], "SAME")
    norm1 = pool1#tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta = 0.75, name='norm1')

    reshapeVal = 25*25*64
    flat  = tf.nn.dropout(tf.reshape(norm1,[-1,reshapeVal]),0.5)

    full_W0 = tf.Variable(tf.truncated_normal([reshapeVal,200],stddev=0.01), name="full_W0")
    full_b0 = tf.Variable(tf.zeros([200]), name="full_b0")
    full_z0 = tf.matmul(flat, full_W0)  + full_b0
    full_u0 = tf.nn.relu(full_z0)

    full_W = tf.Variable(tf.truncated_normal([200,2],stddev=0.01), name="full_W")
    full_b = tf.Variable(tf.zeros([2]), name="full_b")
    full_z = tf.matmul(full_u0, full_W)  + full_b
    full_u = tf.nn.softmax(full_z)

    #conv_L2 = tf.nn.l2_loss(conv_W0) + tf.nn.l2_loss(full_W)

    loss = -tf.reduce_mean(y*tf.log(tf.clip_by_value(full_u,1e-10,1.0))) #+ 0.001*conv_L2
    trainstep = tf.train.AdamOptimizer(1e-5).minimize(loss)
    #trainstep = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
    #trainstep = tf.train.FtrlOptimizer(0.0001).minimize(loss)

    collect_prediction = tf.equal(tf.argmax(full_u, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(collect_prediction,tf.float32))
    
    sess = tf.Session()
    #sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, "model.ckpt")

    #for i in range(label_num):
    output = sess.run(tf.argmax(full_u, 1),feed_dict={x:limit_data,y:limit_label})
    for i in range(label_num):
        if output[i]==label :
            cv2.circle(img,(circles[i][0],circles[i][1]),circles[i][2],(0,255,0),2)
        else:
            cv2.circle(img,(circles[i][0],circles[i][1]),circles[i][2],(0,0,255),2)
    if not os.path.exists("output/"+ file_path):
        os.mkdir("output/"+ file_path)

    
    cv2.imwrite("output/"+ file_path + "/" + datetime.now().strftime('%s') + filename,img)
    sleep(0.1)
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()

    #f = open(resultName+"/TestResult.csv","w")
    #writer = csv.writer(f)
    #writer.writerow(["Name","IsCollect"])
    #isCollect,answer,output,output_data = sess.run((collect_prediction,tf.argmax(y, 1),tf.argmax(full_u, 1),full_u),feed_dict={x:data,y:label})
    #for i in range(TestNum):
    #    writer.writerow([ten[i][0],int(isCollect[i]),answer[i],output[i],output_data[i][0],output_data[i][1]])
    tf.reset_default_graph()

def learning_data_print(dataList):
    data_num = len(dataList[0])
    for i in range(0,data_num,200):
        for j in range(200):
            path = "image/"+str(i/200)+"/"+ dataList[0][i+j][0] +".png"
            if not os.path.exists(path) :
                plt.imshow(dataList[2][i+j])
                plt.savefig(path)
                plt.clf()
            #pilImg = Image.fromarray(np.uint8(dataList[2][i+j]))
            #pilImg.save("image/"+str(i/200)+"/"+ dataList[0][j][0] +".png")
            #print dataList[2][i]
            #plt.show()

if __name__ == '__main__':
    
    #show_img("/home/miyake/Desktop/kahun_ohira/sugi.npy")
    #Test('/home/miyake/Desktop/kahun_ohira/original_data/test/hinoki/IMG_3782.JPG',1)
    #Test('/home/miyake/Desktop/kahun_ohira/original_data/test/hinoki/IMG_3785.JPG',1)
    #Test('/home/miyake/Desktop/kahun_ohira/original_data/test/hinoki/IMG_3792.JPG',1)
    #Test('/home/miyake/Desktop/kahun_ohira/original_data/test/sugi/IMG_3806.JPG',0)
    #Test('/home/miyake/Desktop/kahun_ohira/original_data/test/sugi/IMG_3808.JPG',0)
    #Test('/home/miyake/Desktop/kahun_ohira/original_data/test/sugi/IMG_3806.JPG',0)

    #Test('/home/miyake/Desktop/kahun_ohira/original_data/mix/IMG_3828.JPG',0)
    #sys.exit()

    alldata = GetData()
    #learning_data_print(alldata)
    #show_npy(alldata)
   
    
    data_num = len(alldata[0])
    now = datetime.now()
    for i in range(0,data_num,200):
        testidxList = []
        for j in range(200):
            testidxList.append(i+j)
 
        resultName = now.strftime('%s')+"_Result_"+str(i)+"_"+str(i+200)
        Train(alldata,testidxList,resultName)
        for path, subdirs, files in os.walk("check/"):
            for filename in files:
                f = os.path.join(path, filename)   
                Test(str(f),0,filename,path)
            
        
        #Test('/home/miyake/Desktop/kahun_ohira/original_data/test/sugi/IMG_3802.JPG',0)
        #Test('/home/miyake/Desktop/kahun_ohira/original_data/mix/IMG_3814.JPG',0)

Training from scratch

Is it possible to release the trainer for either VGG16 or VGG 19 from scratch? Also what is a good method for initializing the weights for training from scratch?

train model seems not correct

I want to train the model from the initial state. The images are all 224*224. But the problem is, when I train the model in a FOR loop, the probability is always closed to 50%.( My problem is a two classification problem.)Can you provide the complete codes? Or can you fix my problem?

Generate Image using this?

Is there an example showing how to use this to actually generate an image from a source image using the trainer ?

I have it running and outputting to terminal properly but it is not clear how to feed it an image and get out a different image.

Thanks

MEAN

why the mean of image is [103.939, 116.779, 123.68] in VGG16.py?? THANKS.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.