# print('') # # y = np.load('../data/preprocessed/target.npy') # # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=13) # X = None # y = None # # X_train_cnn = X_train.reshape(-1, 2048, 1, 1) # X_train = None # X_test_cnn = X_test.reshape(-1, 2048, 1, 1) # X_test = None # # y_train_one_hot = to_categorical(y_train, num_classes=no_of_classes) # y_train = None # y_test_one_hot = to_categorical(y_test, num_classes=no_of_classes) # y_test = None # # Setup print('Creating Setup ...') nn_setup.setModel(nn) nn_setup.setDataDirectory( XTrain_directory=os.path.join('setup', model_name, 'XTrain'), XValidation_directory=os.path.join('setup', model_name, 'XTrain'), XTest_directory=os.path.join('setup', model_name, 'XTrain'), YTrain_directory=os.path.join('setup', model_name, 'YTrain'), YValidation_directory=os.path.join('setup', model_name, 'YTrain'), YTest_directory=os.path.join('setup', model_name, 'YTrain')) nn_setup.save('setup')
cnn.add(Dropout(0.25)) cnn.add(Conv2D(64, (8, 8), activation='linear', padding='same')) cnn.add(LeakyReLU(alpha=0.1)) cnn.add(MaxPooling2D(pool_size=(8, 8), padding='same')) cnn.add(Dropout(0.4)) cnn.add(Flatten()) cnn.add(Dense(256, activation='linear')) cnn.add(LeakyReLU(alpha=0.1)) cnn.add(Dropout(0.3)) cnn.add(Dense(num_classes, activation='softmax')) cnn.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.SGD(), metrics=['accuracy']) X_train_cnn = X_train.reshape(-1, 256, 256, 1) X_test_cnn = X_test.reshape(-1, 256, 256, 1) y_train_one_hot = to_categorical(y_train, num_classes=num_classes) y_test_one_hot = to_categorical(y_test, num_classes=num_classes) # # Setup cnn_setup = Setup('cnn_landmark_32-64-128-256_k88') cnn_setup.setModel(cnn) cnn_setup.setData(XTrain=X_train_cnn, YTrain=y_train_one_hot, XValidation=X_test_cnn, YValidation=y_test_one_hot) cnn_setup.save('setup')
class TestSetup(TestCase): @classmethod def setUpClass(cls): (cls.train_X, cls.train_Y), (cls.test_X, cls.test_Y) = fashion_mnist.load_data() (cls.train_X, cls.train_Y), (cls.test_X, cls.test_Y) = (cls.train_X[:100], cls.train_Y[:100]), (cls.test_X[:100], cls.test_Y[:100]) cls.train_X = cls.train_X.reshape(-1, 28, 28, 1) cls.test_X = cls.test_X.reshape(-1, 28, 28, 1) cls.train_X = cls.train_X.astype('float32') cls.test_X = cls.test_X.astype('float32') cls.train_X = cls.train_X / 255. cls.test_X = cls.test_X / 255. # Change the labels from categorical to one-hot encoding train_Y_one_hot = to_categorical(cls.train_Y) test_Y_one_hot = to_categorical(cls.test_Y) cls.train_X, cls.valid_X, train_label, cls.valid_label = train_test_split(cls.train_X, train_Y_one_hot, test_size=0.2, random_state=13) batch_size = 64 epochs = 2 num_classes = 10 cls.fashion_model = Sequential() cls.fashion_model.add(Conv2D(32, kernel_size=(3, 3), activation='linear', input_shape=(28, 28, 1), padding='same')) cls.fashion_model.add(LeakyReLU(alpha=0.1)) cls.fashion_model.add(MaxPooling2D((2, 2), padding='same')) cls.fashion_model.add(Conv2D(64, (3, 3), activation='linear', padding='same')) cls.fashion_model.add(LeakyReLU(alpha=0.1)) cls.fashion_model.add(MaxPooling2D(pool_size=(2, 2), padding='same')) cls.fashion_model.add(Conv2D(128, (3, 3), activation='linear', padding='same')) cls.fashion_model.add(LeakyReLU(alpha=0.1)) cls.fashion_model.add(MaxPooling2D(pool_size=(2, 2), padding='same')) cls.fashion_model.add(Flatten()) cls.fashion_model.add(Dense(128, activation='linear')) cls.fashion_model.add(LeakyReLU(alpha=0.1)) cls.fashion_model.add(Dense(num_classes, activation='softmax')) cls.fashion_model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(), metrics=['accuracy']) cls.fashion_train = cls.fashion_model.fit(cls.train_X, train_label, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(cls.valid_X, cls.valid_label)) cls.test_eval = cls.fashion_model.evaluate(cls.test_X, test_Y_one_hot, verbose=0) def setUp(self): self.setup = Setup('fashion_model') def test_updateEpochs_correctList_noModify(self): mList = [1, 2, 3] self.setup.updateEpochs(3, mList, mList, mList, mList, mList, mList, allow_modify=False) self.assertEqual(self.setup._train_accuracy, [1, 2, 3]) self.assertEqual(self.setup._train_loss, [1, 2, 3]) self.assertEqual(self.setup._val_accuracy, [1, 2, 3]) self.assertEqual(self.setup._val_loss, [1, 2, 3]) self.assertEqual(self.setup._test_accuracy, [1, 2, 3]) self.assertEqual(self.setup._test_loss, [1, 2, 3]) def test_updateEpochs_smallerList_noModify(self): mList = [1, 2, 3] self.assertRaises(ValueError, self.setup.updateEpochs, 3, mList, mList, mList[:-1], mList, mList, mList, allow_modify=False) def test_updateEpochs_largerList_noModify(self): mList = [1, 2, 3] self.assertRaises(ValueError, self.setup.updateEpochs, 3, mList, mList, mList, mList, mList, mList + [4], allow_modify=False) def test_updateEpochs_correctList_modifyAllowed(self): mList = [1, 2, 3] self.setup.updateEpochs(3, mList, mList, mList, mList, mList, mList, allow_modify=True) self.assertEqual(self.setup._train_accuracy, [1, 2, 3]) self.assertEqual(self.setup._train_loss, [1, 2, 3]) self.assertEqual(self.setup._val_accuracy, [1, 2, 3]) self.assertEqual(self.setup._val_loss, [1, 2, 3]) self.assertEqual(self.setup._test_accuracy, [1, 2, 3]) self.assertEqual(self.setup._test_loss, [1, 2, 3]) def test_updateEpochs_smallerList_modifyAllowed(self): mList = [1, 2, 3] self.setup.updateEpochs(3, mList, mList, mList, mList, mList[:-1], mList, allow_modify=True) self.assertEqual(self.setup._train_accuracy, [1, 2, 3]) self.assertEqual(self.setup._train_loss, [1, 2, 3]) self.assertEqual(self.setup._val_accuracy, [1, 2, 3]) self.assertEqual(self.setup._val_loss, [1, 2, 3]) self.assertEqual(self.setup._test_accuracy, [1, 2, 2]) self.assertEqual(self.setup._test_loss, [1, 2, 3]) def test_updateEpochs_largerList_modifyAllowed(self): mList = [1, 2, 3] self.setup.updateEpochs(3, mList, mList, mList, mList, mList, mList + [4], allow_modify=True) self.assertEqual(self.setup._train_accuracy, [1, 2, 3]) self.assertEqual(self.setup._train_loss, [1, 2, 3]) self.assertEqual(self.setup._val_accuracy, [1, 2, 3]) self.assertEqual(self.setup._val_loss, [1, 2, 3]) self.assertEqual(self.setup._test_accuracy, [1, 2, 3]) self.assertEqual(self.setup._test_loss, [1, 2, 3]) def test_positive(self): self.setup.setModel(self.fashion_model) self.setup.setData(XTrain=self.train_X) self.setup.setData(XValidation=self.valid_X) self.setup.setData(XTest=self.test_X) self.setup.setData(YTrain=self.train_Y) self.setup.setData(YValidation=self.valid_label) self.setup.setData(YTest=self.test_Y) self.setup.save('setup')