Demo entry 6655803

main

   

Submitted by anonymous on Oct 28, 2017 at 09:30
Language: Python 3. Code size: 3.7 kB.

###################################################################
# @Purpose:  Predict the Characters in The Simposons 
# @Author:   Tai Rong <t106368030@ntut.edu.tw>
# @Date:     2017/10/25
# @package:  Keras 2.0 , TensorFlow 1.3 , Opencv 3.0
# @Input:    Train Data:    train.zip >> characters-20 (536 MB)
#            Train Label:   character_name.lst
###################################################################
import os,sys
import cv2
import numpy as np
from sklearn.cross_validation import train_test_split
from keras.models import Sequential, Model, load_model
from keras import applications
from keras.layers import *
from keras.callbacks import *
from keras.utils import np_utils
from keras.optimizers import SGD, Adam
from keras.preprocessing.image import ImageDataGenerator

epochs=150
batch_size=256

images = []
labels = []
listdir = []


def read_images(path,i):
    for file in os.listdir(path):
        abs_path = os.path.abspath(os.path.join(path, file)) # abs_path =  C:\\XXX\XXX\ + train\XXX\  ||  +(XXX).jpg 
        if os.path.isdir(abs_path):
            i+=1                                             # 1- 20
            temp=os.path.split(abs_path)[-1]
            listdir.append(temp)                             # C:\\XXX\XXX\ + train\XXX\ >> XXX
            read_images(abs_path,i)
            amount=len(os.listdir(path))
            sys.stdout.write('\r'+'>'*(i*100//amount)+' '*((a-i)*(100//amount) )+'[%s%%]'%(i*100/amount)+temp) #Loading Bar
        else:  
            if file.endswith('.jpg'):
                image=cv2.resize(cv2.imread(abs_path),(64,64))
                images.append(image)
                labels.append(i-1)
    return images, labels ,listdir

def read(path):
    images, labels ,listdir = read_images(path,i=0)
    images=np.array(images,dtype=np.float32)/255
    labels=np_utils.to_categorical(labels, num_classes=20)
    np.savetxt('listdir.txt', listdir, delimiter = ' ',fmt="%s")
    return images, labels

images, labels=read('train')
X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.1)

print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)

model = Sequential()
model.add(Conv2D(64, kernel_size=3 , padding='same',activation='relu', input_shape=X_train.shape[1:]))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Conv2D(128, kernel_size=3, padding='same',activation='relu'))
model.add(Conv2D(128, kernel_size=3, padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Conv2D(256, kernel_size=3, padding='same',activation='relu'))
model.add(Conv2D(256, kernel_size=3, padding='same',activation='relu'))
model.add(Conv2D(256, kernel_size=3, padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(20,activation='softmax'))

model.summary()
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])

datagen = ImageDataGenerator(zoom_range=0.2,width_shift_range=0.2,height_shift_range=0.2,horizontal_flip=True,)
datagen.fit(X_train)

file_name=str(epochs)+'_'+str(batch_size)
TB=TensorBoard(log_dir='logs/'+file_name,histogram_freq = 1,write_images = 1,)
model.fit(X_train, y_train,epochs=epochs,batch_size=batch_size,shuffle=True,verbose=1,validation_data=(X_test, y_test),callbacks=[TB])
model.save('h5/'+file_name+'.h5')

score = model.evaluate(X_test, y_test, verbose=0)
print(score)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).