Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import PIL
- import PIL.Image
- import PIL.ImageFont
- import PIL.ImageOps
- import PIL.ImageDraw
- import pandas as pd
- import numpy as np
- import os,cv2
- import matplotlib.pyplot as plt
- import keras
- import sklearn
- from sklearn import preprocessing
- from sklearn.utils import shuffle
- from sklearn.cross_validation import train_test_split
- from keras import backend as K
- K.set_image_dim_ordering('th')
- from keras.utils import np_utils
- from keras.models import Sequential
- from keras.layers.core import Dense, Dropout, Activation, Flatten
- from keras.layers.convolutional import Convolution2D, MaxPooling2D
- from keras.optimizers import SGD,RMSprop,adam
- import scipy
- from skimage import io
- %matplotlib inline
- #img_rows=64
- #img_cols=64
- num_channel=1
- num_epoch=10
- labels =[]
- image=[]
- for i in range (50000):
- img = cv2.imread("E:/data/pic/%s.png"%('{0:06}'.format(i)))
- img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- #img_resize=cv2.resize(img,(180,670))
- image.append(img)
- img_data = np.array(image)
- img_data = img_data.astype('float32')
- img_data /= 255
- print (img_data.shape)
- if num_channel==1:
- if K.image_dim_ordering()=='th':
- img_data= np.expand_dims(img_data, axis=1)
- print (img_data.shape)
- else:
- img_data= np.expand_dims(img_data, axis=4)
- print (img_data.shape)
- else:
- if K.image_dim_ordering()=='th':
- img_data=np.rollaxis(img_data,3,1)
- print (img_data.shape)
- #%%
- # Assigning Labels
- # Define the number of classes
- num_classes = 5
- num_of_samples = img_data.shape[0]
- labels = np.ones((num_of_samples,),dtype='int64')
- for i in range (50000):
- with open('E:/data/font/%s.meta'%('{0:06}'.format(i)), 'r') as f:
- temp = f.readline()
- first_line = temp.strip()
- if(first_line=='Alegreya-Regular'):
- labels[i] = 0
- elif(first_line=='Alegreya-Bold'):
- labels[i] = 1
- elif(first_line=='basic_sans_serif'):
- labels[i] = 2
- elif(first_line=='bold_sans_serif'):
- labels[i] = 3
- elif(first_line=='ChargerMoSp'):
- labels[i] = 4
- names = ['Alegreya-Regular','Alegreya-Bold','basic_sans_serif','bold_sans_serif','ChargerMoSp']
- # convert class labels to on-hot encoding
- Y = np_utils.to_categorical(labels, num_classes)
- #Shuffle the dataset
- x,y = shuffle(img_data,Y, random_state=2)
- # Split the dataset
- X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
- model = Sequential()
- model.add(Convolution2D(16, kernel_size=(5, 5), strides=(1, 1),activation='relu',input_shape=(1,180,670)))
- model.add(Convolution2D(16, kernel_size=(5, 5), strides=(1, 1),activation='relu'))
- keras.layers.normalization.BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,scale=False, beta_initializer='zeros',gamma_initializer='ones', moving_mean_initializer='zeros',moving_variance_initializer='ones',beta_regularizer=None, gamma_regularizer=None,beta_constraint=None, gamma_constraint=None)
- model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
- model.add(Convolution2D(16, (5, 5), activation='relu'))
- model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
- keras.layers.normalization.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True,scale=False, beta_initializer='zeros',gamma_initializer='ones', moving_mean_initializer='zeros',moving_variance_initializer='ones',beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None)
- model.add(Convolution2D(16, (3, 3), activation='relu'))
- keras.layers.normalization.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True,scale=False, beta_initializer='zeros',gamma_initializer='ones', moving_mean_initializer='zeros',moving_variance_initializer='ones',beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None)
- model.add(MaxPooling2D(pool_size=(2, 2)))
- model.add(Convolution2D(32, (3, 3), activation='relu'))
- model.add(MaxPooling2D(pool_size=(2, 2)))
- model.add(Dropout(0.5))
- model.add(Flatten())
- model.add(Dense(64, activation='relu'))
- model.add(Dropout(0.5))
- model.add(Dense(num_classes, activation='softmax'))
- #sgd = SGD(lr=learning_rate, decay=decay, momentum=0.9, nesterov=False)
- model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=False),metrics=['accuracy'])
- # Viewing model_configuration
- model.summary()
- model.get_config()
- model.layers[0].get_config()
- model.layers[0].input_shape
- model.layers[0].output_shape
- model.layers[0].get_weights()
- np.shape(model.layers[0].get_weights()[0])
- model.layers[0].trainable
- # Training
- hist = model.fit(X_train, y_train, batch_size=16, nb_epoch=num_epoch, verbose=1, validation_data=(X_test, y_test))
- #hist = model.fit(X_train, y_train, batch_size=32, nb_epoch=20,verbose=1, validation_split=0.2)
- # visualizing losses and accuracy
- train_loss=hist.history['loss']
- val_loss=hist.history['val_loss']
- train_acc=hist.history['acc']
- val_acc=hist.history['val_acc']
- xc=range(num_epoch)
- score = model.evaluate(X_test, y_test, verbose=0)
- print('Test Loss:', score[0])
- print('Test accuracy:', score[1])
- test_image = X_test[0:1]
- print (test_image.shape)
- print(model.predict(test_image))
- print(model.predict_classes(test_image))
- print(y_test[0:1])
- # Testing a new image
- test_image = cv2.imread('C:/Users/Parvin.E/Desktop/data2/pic/000085.png')
- test_image=cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)
- #test_image=cv2.resize(test_image,(64,64))
- test_image = np.array(test_image)
- test_image = test_image.astype('float32')
- test_image /= 255
- print (test_image.shape)
- if num_channel==1:
- if K.image_dim_ordering()=='th':
- test_image= np.expand_dims(test_image, axis=0)
- test_image= np.expand_dims(test_image, axis=0)
- print (test_image.shape)
- else:
- test_image= np.expand_dims(test_image, axis=3)
- test_image= np.expand_dims(test_image, axis=0)
- print (test_image.shape)
- else:
- if K.image_dim_ordering()=='th':
- test_image=np.rollaxis(test_image,2,0)
- test_image= np.expand_dims(test_image, axis=0)
- print (test_image.shape)
- else:
- test_image= np.expand_dims(test_image, axis=0)
- print (test_image.shape)
- # Predicting the test image
- print((model.predict(test_image)))
- print(model.predict_classes(test_image))
Add Comment
Please, Sign In to add comment