def build_model(): model = Sequential() model.add(RandomRotation(factor=0.45)) model.add(InputLayer(input_shape=(224, 224, 1))) model.add(Conv2D(1, (2, 2), activation='relu', padding='same')) model.add(Conv2D(32, (2, 2), strides=2, activation='relu', padding='same')) model.add(Conv2D(64, (2, 2), strides=2, activation='relu', padding='same')) model.add(Conv2D(128, (2, 2), strides=2, activation='relu', padding='same')) model.add( Conv2DTranspose(128, (2, 2), strides=2, activation='relu', padding='same')) model.add( Conv2DTranspose(64, (2, 2), strides=2, activation='relu', padding='same')) model.add( Conv2DTranspose(2, (2, 2), strides=2, activation='relu', padding='same')) return model
def create_and_train_model(): #Importing data mnist = keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() #Normalizing the input x_train = keras.utils.normalize(x_train, axis=1) x_test = keras.utils.normalize(x_test, axis=1) #reshape the arrays for the CNN x_train_r = np.expand_dims(x_train, axis=3) x_test_r = np.expand_dims(x_test, axis=3) #Translating and Rotating the training set data_augmentation = Sequential([ RandomTranslation(0.05, 0.05, fill_mode='constant'), RandomRotation(0.02, fill_mode='constant') ]) x_train_r = data_augmentation(x_train_r) model = Sequential() model.add( Conv2D(32, kernel_size=5, padding='same', activation='relu', input_shape=(28, 28, 1))) model.add(MaxPooling2D()) model.add(Dropout(0.2)) model.add(Conv2D(64, kernel_size=5, padding='same', activation='relu')) model.add(MaxPooling2D(padding='same')) model.add(Dropout(0.2)) model.add(Flatten()) model.add(Dense(256, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(10, activation='softmax')) model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=1, mode='auto', restore_best_weights=True) model.fit(x_train_r, y_train, validation_data=(x_test_r, y_test), callbacks=[monitor], verbose=1, epochs=20) model.save('number_recognition_CNN')
def preprocessing_layers(a, hp): #a = preprocess_input(a)#Rescaling(1/255)(a) translate = hp.Float("translate", 0, 0.5, step=0.1, default=0.2) a = RandomTranslation( #hp.Float("t_x", 0, 0.5, step=0.1, default=0.2), #hp.Float("t_y", 0, 0.5, step=0.1, default=0.2), translate, translate, fill_mode="reflect",interpolation="bilinear",)(a) a = RandomFlip()(a) a = RandomZoom(hp.Float("zoom", 0, 0.5, step=0.1, default=0.25))(a) a = RandomRotation(hp.Float("rotation", 0, 2, step=0.1, default=2))(a) #a = RandomHeight(0.2)(a) #a = RandomWidth(0.2)(a) return a
def get_data(path, num_classes): #the value passed in split to get training and testing is as per the requirement mentioned #for caltech101 and caltech256 dataset it gets randomly 30 images from class for training and remaining for testing #fetch training data train = tf.keras.preprocessing.image_dataset_from_directory( directory=path, labels="inferred", label_mode="int", validation_split=0.6654, subset="training", seed=123, image_size=(224, 224), batch_size=1) #fetch testing data test = tf.keras.preprocessing.image_dataset_from_directory( directory=path, labels="inferred", label_mode="int", validation_split=0.6654, subset="validation", seed=123, image_size=(224, 224), batch_size=1) #noramlize the data normalization = Rescaling(1. / 255) #data agumentation train_aug = Sequential([ Rescaling(1. / 255), RandomFlip("horizontal_and_vertical"), RandomRotation(0.2) ]) #normalize train and test data apply one hot encoding on labels and squeeze train and test to its required dimension #apply train_aug on train set and normalization only on test set #becomes equal to the shape of the input to the convolution network train_n = train.map(lambda x, y: (tf.squeeze(train_aug(x)), tf.squeeze(tf.one_hot(y, num_classes)))) test_n = test.map(lambda x, y: (tf.squeeze(normalization(x)), tf.squeeze(tf.one_hot(y, num_classes)))) #the output of train_n and test_n is mapped dataset so we iterate over the train_n and test_n #and store the features and labels in list train_inputs = [] #for training features train_labels = [] #for training labels test_inputs = [] #for testing features test_labels = [] #for testing labels c = 0 tr = iter(train_n) #create train_n iterator tt = iter(test_n) #create test_n iterator for i in range(0, len(test_n)): if c < len(train_n): i, j = next(tr) train_inputs.append(i) train_labels.append(j) c += 1 i, j = next(tt) test_inputs.append(i) test_labels.append(j) #convert the list to tensor train_inputs = tf.convert_to_tensor(train_inputs, dtype=tf.float32) train_labels = tf.convert_to_tensor(train_labels, dtype=tf.float32) test_inputs = tf.convert_to_tensor(test_inputs, dtype=tf.float32) test_labels = tf.convert_to_tensor(test_labels, dtype=tf.float32) #free memory del train_n, test_n, tr, tt, train, test return train_inputs, train_labels, test_inputs, test_labels
#hp.Float("t_x", 0, 0.5, step=0.1, default=0.2), #hp.Float("t_y", 0, 0.5, step=0.1, default=0.2), translate, translate, fill_mode="reflect",interpolation="bilinear",)(a) a = RandomFlip()(a) a = RandomZoom(hp.Float("zoom", 0, 0.5, step=0.1, default=0.25))(a) a = RandomRotation(hp.Float("rotation", 0, 2, step=0.1, default=2))(a) #a = RandomHeight(0.2)(a) #a = RandomWidth(0.2)(a) return a data_augmentation = Sequential([ RandomFlip("horizontal_and_vertical"), RandomRotation((0,0.5), fill_mode='constant') ]) def comparisons_model(hp): img_size=224 weights=None """ Create comparisons network which reproduce the choice in an images duel. :param img_size: size of input images during training :type img_size: tuple(int) :param weights: path to the weights use for initialization :type weights: str :return: ranking comparisons model :rtype: keras.Model """
# X_train,x_test,Y_train,Y_test = train_test_split(X_train,Y_train,test_size=0.2, random_state = 42) # One hot coding from sklearn import preprocessing lb = preprocessing.LabelBinarizer() Y_train = lb.fit_transform(Y_train) class_num = Y_train.shape[1] # print(Y_train) # Creating the model model = Sequential() # Adding data augmentation model.add(RandomContrast([0.5,1.5])) model.add(RandomFlip('horizontal_and_vertical')) model.add(RandomRotation([0.5,1.5])) model.add(Resizing(128,128,interpolation='bicubic')) model.add(RandomZoom([0.2,0.5])) model.add(RandomTranslation(height_factor=(-0.2,0.2), width_factor=(-0.2,0.2))) #first convolutional layer model.add(Conv2D(32, (5,5), input_shape=(48,48,1), activation='relu', padding='same')) # model.add(Conv2D(32, (5,5), input_shape=(48,48,1), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(BatchNormalization()) #second convolutional layer model.add(Conv2D(64,(5,5), activation='relu', padding='same')) model.add(Conv2D(64,(5,5), activation='relu', padding='same')) # model.add(Conv2D(64,(5,5), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2,2)))