Last active
May 23, 2017 05:30
-
-
Save BeMg/417c9d4e1bdab630a453d0cddea9e9d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| # In[1]: | |
| import tensorflow as tf | |
| from keras.backend.tensorflow_backend import set_session | |
| config = tf.ConfigProto() | |
| config.gpu_options.allow_growth = True | |
| set_session(tf.Session(config=config)) | |
| batch_size = 10 | |
| epochs_num = 20 | |
| height = 192 | |
| weight = 168 | |
| import os | |
| import csv | |
| path = os.getcwd() | |
| data_path = '/Tooth_Brushing_Dataset/image_data/train.csv' | |
| Data_path = [] | |
| with open(path+data_path, newline='') as csvfile: | |
| spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') | |
| for row in spamreader: | |
| x,y = str(row[0]).split(',') | |
| Data_path.append('{} {}\n'.format(path+'/Tooth_Brushing_Dataset/image_data/train/'+str(x)+'.jpg', y)) | |
| Train_file = open('Train_list.txt','w') | |
| Test_file = open('Test_list.txt', 'w') | |
| Train_num = 6000 | |
| Test_num = 1316 | |
| print(len(Data_path)) | |
| for i in range(len(Data_path)): | |
| if i<Train_num: | |
| Train_file.write(Data_path[i]) | |
| else: | |
| Test_file.write(Data_path[i]) | |
| Train_file.close() | |
| Test_file.close() | |
| # In[2]: | |
| import numpy as np | |
| from PIL import Image | |
| def path2img(path): | |
| im = Image.open(path) | |
| img = np.array(im, 'uint8') | |
| return img | |
| # In[3]: | |
| from keras.utils import to_categorical | |
| from keras.preprocessing.image import ImageDataGenerator | |
| def gen_sample_from_file(path, batch_size): | |
| while True: | |
| f = open(path) | |
| Batch_x = [] | |
| Batch_y = [] | |
| cnt = 0 | |
| for line in f: | |
| cnt = cnt + 1 | |
| if cnt % batch_size == 0: | |
| Batch_x = np.array(Batch_x, dtype=np.float32) | |
| Batch_y = np.array(Batch_y, dtype=np.int32) | |
| std = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True) | |
| std.fit(Batch_x) | |
| Batch_x = std.standardize(Batch_x) | |
| yield (Batch_x, Batch_y) | |
| Batch_x = [] | |
| Batch_y = [] | |
| img_path, y = line.split() | |
| im = Image.open(img_path) | |
| im = im.resize([height, weight]) | |
| img = np.array(im, 'uint') | |
| img = np.reshape(img, [height, weight, 3]) | |
| y = int(y)-1 | |
| y = to_categorical(y, num_classes=16) | |
| y = np.reshape(y, [16]) | |
| Batch_x.append(img) | |
| Batch_y.append(y) | |
| f.close() | |
| # In[ ]: | |
| # In[ ]: | |
| from keras.models import Sequential | |
| from keras.layers import * | |
| from keras.optimizers import SGD | |
| model = Sequential() | |
| model.add(Conv2D(96, kernel_size=(11, 11), activation='relu', input_shape=(height, weight, 3))) | |
| model.add(MaxPooling2D(pool_size=(3, 3))) | |
| model.add(Conv2D(256, kernel_size=(5, 5), activation='relu')) | |
| model.add(MaxPooling2D(pool_size=(3, 3))) | |
| model.add(Conv2D(384, kernel_size=(5, 5))) | |
| model.add(Conv2D(384, kernel_size=(5, 5))) | |
| model.add(Conv2D(256, kernel_size=(5, 5))) | |
| model.add(MaxPooling2D(pool_size=(3, 3))) | |
| model.add(Flatten()) | |
| model.add(Dense(1024)) | |
| model.add(Dropout(0.3)) | |
| model.add(Dense(1024)) | |
| model.add(Dense(16, activation='softmax')) | |
| sgd = SGD(lr=0.001, decay=0.01, momentum=0.9, nesterov=True) | |
| model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) | |
| model.summary() | |
| hist = model.fit_generator(generator = gen_sample_from_file('Train_list.txt', batch_size),validation_data= gen_sample_from_file('Test_list.txt', batch_size), validation_steps = 10 , steps_per_epoch = 600, epochs = epochs_num) | |
| model.save_weights('my_model_weights.h5') | |
| from keras.backend import clear_session | |
| clear_session() | |
| # In[ ]: | |
| f = open('lossfile.txt','w') | |
| f.write('Loss') | |
| for i in hist.history['loss']: | |
| f.write(str(i)+'\n') | |
| f.write('val_loss') | |
| for i in hist.history['val_loss']: | |
| f.write(str(i)+'\n') | |
| f.write('acc') | |
| for i in hist.history['acc']: | |
| f.write(str(i)+'\n') | |
| f.write('val_acc') | |
| for i in hist.history['val_acc']: | |
| f.write(str(i)+'\n') | |
| f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment