Esempio n. 1
0
	def test(self):
		'''

		use self.clf to get score/accuracy
		prints accuracy
		draws plot
		'''
		img_rows,img_columns = 45,45
		test_data = self.test_img.reshape((self.test_img.shape[0], img_rows,img_columns))
		test_data = test_data[:, np.newaxis, :, :]
		label_map={}
		count = 0
		for folder in os.listdir("./data"):
			label_map[folder]=count
			count+=1
		for j in range(len(self.test_labels)):
			self.test_labels[j]=label_map[self.test_labels[j]]
		total_classes = count
		test_labels = np_utils.to_categorical(self.test_labels,total_classes)
		sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
		clf = CNN().build(img_rows,img_columns,1,total_classes,'model.h5')
		clf.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])
		loss, accuracy = clf.evaluate(test_data, test_labels, batch_size=self.b_size, verbose=1)
		print('Accuracy of Model: {:.2f}%'.format(accuracy * 100))
		return accuracy
def main():
    # data generator
    data_generator = ImageDataGenerator(
        featurewise_center=False,
        featurewise_std_normalization=False,
        rotation_range=0,
        width_shift_range=0.1,
        height_shift_range=0.1,
        zoom_range=.1,
        horizontal_flip=True)

    model = CNN()
    opt = optimizers.Adam(lr=0.0001)
    # opt = optimizers.SGD(lr=0.001)

    model.compile(opt, loss='categorical_crossentropy', metrics=['accuracy'])
    model.summary()

    # callbacks
    f = open(base_path + 'gender_classification_training.log', 'w')
    f.close()
    log_file_path = base_path + 'gender_classification_training.log'
    csv_logger = CSVLogger(log_file_path, append=False)
    early_stop = EarlyStopping('val_loss', patience=patience)
    reduce_lr = ReduceLROnPlateau('val_loss', factor=0.1, patience=int(patience/4), verbose=1)

    trained_models = base_path + 'CNN.{epoch:02d}-{val_loss:.3f}-{val_acc:.2f}.hdf5'
    # model_cp = ModelCheckpoint(trained_models, 'val_acc', verbose=1, save_best_only=True)
    model_cp = ModelCheckpoint(trained_models, 'val_loss', verbose=1, save_best_only=True)
    callbacks = [model_cp, csv_logger, early_stop, reduce_lr]

    # load data
    faces, labels = load_data(data_path)
    print (len(faces))
    print (len(labels))
    faces = preprocess_input(faces)
    order = np.argsort(np.random.random(len(faces)))
    faces = faces[order]
    labels = labels[order]

    train_data, val_data = split_data(faces, labels, validation_split)
    train_faces, train_labels = train_data
    model.fit_generator(data_generator.flow(train_faces, train_labels, batch_size),
                        steps_per_epoch=len(train_faces)/batch_size,
                        epochs=num_epochs,
                        verbose=1,
                        callbacks=callbacks,
                        validation_data=val_data)
Esempio n. 3
0
	def train(self):

		#Change data to required format
		img_rows,img_columns = 45,45
		train_data = self.train_img.reshape((self.train_img.shape[0], img_rows,img_columns))
		train_data = train_data[:, np.newaxis, :, :]
		test_data = self.test_img.reshape((self.test_img.shape[0], img_rows,img_columns))
		test_data = test_data[:, np.newaxis, :, :]
		label_map={}
		count = 0
		for folder in os.listdir("../src/data"):
			label_map[folder]=count
			count+=1
		for i in range(len(self.train_labels)):
			self.train_labels[i]=label_map[self.train_labels[i]]
		for j in range(len(self.test_labels)):
			self.test_labels[j]=label_map[self.test_labels[j]]

		# Transform training and testing data to 10 classes in range [0,classes] ; num. of classes = 0 to 9 = 10 classes
		total_classes = count
		train_labels = np_utils.to_categorical(self.train_labels, total_classes)
		test_labels = np_utils.to_categorical(self.test_labels,total_classes)


		# Defing and compile the SGD optimizer and CNN model
		print('\n Compiling model...')
		sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
		clf = CNN().build(img_rows,img_columns,1,total_classes)
		clf.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])

		# Initially train and test the model; If weight saved already, load the weights using arguments.
		num_epoch = self.num_epoch		# Number of epochs
		verb = 1			# Verbose
		print('\nTraining the Model...')
		model_info=clf.fit(train_data, train_labels, batch_size=self.b_size, nb_epoch=num_epoch,verbose=verb)

		# Evaluate accuracy and loss function of test data
		print('Evaluating Accuracy and Loss Function...')
		loss, accuracy = clf.evaluate(test_data, test_labels, batch_size=self.b_size, verbose=1)
		print('Accuracy of Model',(accuracy * 100))
		clf.save_weights('model.h5', overwrite=True)
		print(model_info.history)
		self.plot_model_history(model_info)
		return accuracy
Esempio n. 4
0
def run():
    (X_train, y_train), (X_test, y_test) = datasets.load_data(img_rows=32,
                                                              img_cols=32)
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)
    model = CNN(input_shape=X_train.shape[1:], nb_classes=nb_classes)
    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])
    X_train = preprocess_input(X_train)
    X_test = preprocess_input(X_test)
    csv_logger = CSVLogger('../log/cnn.log')
    checkpointer = ModelCheckpoint(filepath="/tmp/weights.hdf5",
                                   monitor="val_acc",
                                   verbose=1,
                                   save_best_only=True)
    datagen = ImageDataGenerator(
        featurewise_center=False,  # set input mean to 0 over the dataset
        samplewise_center=False,  # set each sample mean to 0
        featurewise_std_normalization=
        False,  # divide inputs by std of the dataset
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # apply ZCA whitening
        rotation_range=
        0,  # randomly rotate images in the range (degrees, 0 to 180)
        width_shift_range=
        0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=
        0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False)  # randomly flip images
    datagen.fit(X_train)
    model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
                        samples_per_epoch=X_train.shape[0],
                        nb_epoch=nb_epoch,
                        validation_data=(X_test, Y_test),
                        callbacks=[csv_logger, checkpointer])
ds_train = tf.data.Dataset.list_files("data/train/*/*.jpg") \
        .map(load_img,num_parallel_calls=tf.data.experimental.AUTOTUNE) \
        .shuffle(buffer_size=1000).batch(BATCH_SIZE) \
        .prefetch(tf.data.experimental.AUTOTUNE)

ds_test = tf.data.Dataset.list_files("data/test/*/*.jpg") \
        .map(load_img,num_parallel_calls=tf.data.experimental.AUTOTUNE) \
        .shuffle(buffer_size=1000).batch(BATCH_SIZE) \
        .prefetch(tf.data.experimental.AUTOTUNE)

# 获得模型
model = CNN()
# 编译模型
model.compile(
        optimizer='adam',
        loss='binary_crossentropy',
        metrics=["accuracy"]
    )
# 训练中每个世代保存一次
cp_callback = ModelCheckpoint(
        'logs/ep{epoch:02d}-loss{loss:.2f}.h5',
        monitor='acc',save_weights_only=True, 
    )
# 训练模型
history = model.fit(ds_train,epochs=EPOCH,
        validation_data=ds_test,
        callbacks=[cp_callback],
    )
# 保存最终模型
model.save_weights('logs/last1.h5')
Esempio n. 6
0
def main():

    (X_train, y_train), (X_test,
                         y_test) = imdb.load_data(num_words=MAX_NUM_WORDS)
    X = np.concatenate((X_train, X_test), axis=0)
    y = np.concatenate((y_train, y_test), axis=0)
    labels = to_categorical(y)
    print('Training samples: %i' % len(X))

    # docs   = negative_docs + positive_docs
    # labels = [0 for _ in range(len(negative_docs))] + [1 for _ in range(len(positive_docs))]

    # labels = to_categorical(labels)
    # print('Training samples: %i' % len(docs))

    # tokenizer.fit_on_texts(docs)
    # sequences = tokenizer.texts_to_sequences(docs)

    # word_index = tokenizer.word_index

    result = [len(x) for x in X]
    print('Text informations:')
    print(
        'max length: %i / min length: %i / mean length: %i / limit length: %i'
        % (np.max(result), np.min(result), np.mean(result), MAX_SEQ_LENGTH))
    # print('vacobulary size: %i / limit: %i' % (len(word_index), MAX_NUM_WORDS))

    # Padding all sequences to same length of `MAX_SEQ_LENGTH`
    data = pad_sequences(X, maxlen=MAX_SEQ_LENGTH, padding='post')

    histories = []

    for i in range(RUNS):
        print('Running iteration %i/%i' % (i + 1, RUNS))
        random_state = np.random.randint(1000)

        X_train, X_val, y_train, y_val = train_test_split(
            data, labels, test_size=VAL_SIZE, random_state=random_state)

        emb_layer = None
        # if USE_GLOVE:
        #     emb_layer = create_glove_embeddings()

        model = CNN(embedding_layer=emb_layer,
                    num_words=MAX_NUM_WORDS,
                    embedding_dim=EMBEDDING_DIM,
                    filter_sizes=FILTER_SIZES,
                    feature_maps=FEATURE_MAPS,
                    max_seq_length=MAX_SEQ_LENGTH,
                    dropout_rate=DROPOUT_RATE,
                    hidden_units=HIDDEN_UNITS,
                    nb_classes=NB_CLASSES).build_model()

        model.compile(loss='categorical_crossentropy',
                      optimizer=optimizers.Adam(),
                      metrics=['accuracy'])

        if i == 0:
            print(model.summary())
            plot_model(model,
                       to_file='cnn_model.png',
                       show_layer_names=False,
                       show_shapes=True)

        history = model.fit(
            X_train,
            y_train,
            epochs=NB_EPOCHS,
            batch_size=BATCH_SIZE,
            verbose=1,
            validation_data=(X_val, y_val),
            callbacks=[
                # TQDMCallback(),
                ModelCheckpoint('model-%i.h5' % (i + 1),
                                monitor='val_loss',
                                verbose=1,
                                save_best_only=True,
                                mode='min'),
                # TensorBoard(log_dir='./logs/temp', write_graph=True)
            ])
        print()
        histories.append(history.history)

    with open('history.pkl', 'wb') as f:
        pickle.dump(histories, f)
    histories = pickle.load(open('history.pkl', 'rb'))

    def get_avg(histories, his_key):
        tmp = []
        for history in histories:
            tmp.append(history[his_key][np.argmin(history['val_loss'])])
        return np.mean(tmp)

    print('Training: \t%0.4f loss / %0.4f acc' %
          (get_avg(histories, 'loss'), get_avg(histories, 'acc')))
    print('Validation: \t%0.4f loss / %0.4f acc' %
          (get_avg(histories, 'val_loss'), get_avg(histories, 'val_acc')))

    plot_acc_loss('training', histories, 'acc', 'loss')
    plot_acc_loss('validation', histories, 'val_acc', 'val_loss')
Esempio n. 7
0
# Divide data into testing and training sets.
train_img, test_img, train_labels, test_labels = cross_validation.train_test_split(mnist_data/255.0, dataset.target.astype("int"), test_size=0.1)

# Now each image rows and columns are of 28x28 matrix type.
img_rows,img_columns = 28,28

# Transform training and testing data to 10 classes in range [0,classes] ; num. of classes = 0 to 9 = 10 classes
total_classes = 10			# 0 to 9 labels
train_labels = np_utils.to_categorical(train_labels, 10)
test_labels = np_utils.to_categorical(test_labels, 10)

# Defing and compile the SGD optimizer and CNN model
print('\n Compiling model...')
sgd = optimisers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
clf = CNN(width=img_rows, height=img_columns, depth=1, total_classes=total_classes, weightsPath=args["weights"])
clf.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])

# Initially train and test the model; If weight saved already, load the weights using arguments.
b_size = 128		# Batch size
num_epoch = 20		# Number of epochs
verb = 1			# Verbose

# If weights saved and argument load_model; Load the pre-trained model.
if args["load_model"] < 0:
	print('\nTraining the Model...')
	clf.fit(trainData, trainLabels, batch_size=b_size, nb_epoch=num_epoch,verbose=verb)
	
	# Evaluate accuracy and loss function of test data
	print('Evaluating Accuracy and Loss Function...')
	loss, accuracy = clf.evaluate(test_img, test_labels, batch_size=128, verbose=1)
	print('Accuracy of Model: {:.2f}%'.format(accuracy * 100))