def train_networks(training_percentage, filename, experiment): stages = constants.training_stages (data, labels) = get_data(experiment, one_hot=True) total = len(data) step = total/stages # Amount of training data, from which a percentage is used for # validation. training_size = int(total*training_percentage) histories = [] for n in range(stages): i = int(n*step) j = (i + training_size) % total training_data = get_data_in_range(data, i, j) training_labels = get_data_in_range(labels, i, j) testing_data = get_data_in_range(data, j, i) testing_labels = get_data_in_range(labels, j, i) truly_training = int(training_size*truly_training_percentage) validation_data = training_data[truly_training:] validation_labels = training_labels[truly_training:] training_data = training_data[:truly_training] training_labels = training_labels[:truly_training] input_img = Input(shape=(img_columns, img_rows, 1)) encoded = get_encoder(input_img) classified = get_classifier(encoded) decoded = get_decoder(encoded) model = Model(inputs=input_img, outputs=[classified, decoded]) model.compile(loss=['categorical_crossentropy', 'mean_squared_error'], optimizer='adam', metrics='accuracy') model.summary() history = model.fit(training_data, (training_labels, training_data), batch_size=batch_size, epochs=epochs, validation_data= (validation_data, {'classification': validation_labels, 'autoencoder': validation_data}), callbacks=[EarlyStoppingAtLossCrossing(patience)], verbose=2) histories.append(history) history = model.evaluate(testing_data, (testing_labels, testing_data),return_dict=True) histories.append(history) model.save(constants.model_filename(filename, n)) return histories
def remember(experiment, occlusion = None, bars_type = None, tolerance = 0): """ Creates images from features. Uses the decoder part of the neural networks to (re)create images from features. Parameters ---------- experiment : TYPE DESCRIPTION. occlusion : TYPE, optional DESCRIPTION. The default is None. tolerance : TYPE, optional DESCRIPTION. The default is 0. Returns ------- None. """ for i in range(constants.training_stages): testing_data_filename = constants.data_name + constants.testing_suffix testing_data_filename = constants.data_filename(testing_data_filename, i) testing_features_filename = constants.features_name(experiment, occlusion, bars_type) + constants.testing_suffix testing_features_filename = constants.data_filename(testing_features_filename, i) testing_labels_filename = constants.labels_name + constants.testing_suffix testing_labels_filename = constants.data_filename(testing_labels_filename, i) memories_filename = constants.memories_name(experiment, occlusion, bars_type, tolerance) memories_filename = constants.data_filename(memories_filename, i) labels_filename = constants.labels_name + constants.memory_suffix labels_filename = constants.data_filename(labels_filename, i) model_filename = constants.model_filename(constants.model_name, i) testing_data = np.load(testing_data_filename) testing_features = np.load(testing_features_filename) testing_labels = np.load(testing_labels_filename) memories = np.load(memories_filename) labels = np.load(labels_filename) model = tf.keras.models.load_model(model_filename) # Drop the classifier. autoencoder = Model(model.input, model.output[1]) autoencoder.summary() # Drop the encoder input_mem = Input(shape=(constants.domain, )) decoded = get_decoder(input_mem) decoder = Model(inputs=input_mem, outputs=decoded) decoder.summary() for dlayer, alayer in zip(decoder.layers[1:], autoencoder.layers[11:]): dlayer.set_weights(alayer.get_weights()) produced_images = decoder.predict(testing_features) n = len(testing_labels) Parallel(n_jobs=constants.n_jobs, verbose=5)( \ delayed(store_images)(original, produced, constants.testing_directory(experiment, occlusion, bars_type), i, j, label) \ for (j, original, produced, label) in \ zip(range(n), testing_data, produced_images, testing_labels)) total = len(memories) steps = len(constants.memory_fills) step_size = int(total/steps) for j in range(steps): print('Decoding memory size ' + str(j) + ' and stage ' + str(i)) start = j*step_size end = start + step_size mem_data = memories[start:end] mem_labels = labels[start:end] produced_images = decoder.predict(mem_data) Parallel(n_jobs=constants.n_jobs, verbose=5)( \ delayed(store_memories)(label, produced, features, constants.memories_directory(experiment, occlusion, bars_type, tolerance), i, j) \ for (produced, features, label) in zip(produced_images, mem_data, mem_labels))
def obtain_features(model_prefix, features_prefix, labels_prefix, data_prefix, training_percentage, am_filling_percentage, experiment, occlusion = None, bars_type = None): """ Generate features for images. Uses the previously trained neural networks for generating the features corresponding to the images. It may introduce occlusions. """ stages = constants.training_stages (data, labels) = get_data(experiment, occlusion, bars_type) total = len(data) step = int(total/constants.training_stages) training_size = int(total*training_percentage) filling_size = int(total*am_filling_percentage) testing_size = total - training_size - filling_size histories = [] for n in range(stages): i = int(n*step) j = (i+training_size) % total training_data = get_data_in_range(data, i, j) training_labels = get_data_in_range(labels, i, j) k = (j+filling_size) % total filling_data = get_data_in_range(data, j, k) filling_labels = get_data_in_range(labels, j, k) l = (k+testing_size) % total testing_data = get_data_in_range(data, k, l) testing_labels = get_data_in_range(labels, k, l) # Recreate the exact same model, including its weights and the optimizer model = tf.keras.models.load_model(constants.model_filename(model_prefix, n)) # Drop the autoencoder and the last layers of the full connected neural network part. classifier = Model(model.input, model.output[0]) no_hot = to_categorical(testing_labels) classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics='accuracy') history = classifier.evaluate(testing_data, no_hot, batch_size=batch_size, verbose=1, return_dict=True) print(history) histories.append(history) model = Model(classifier.input, classifier.layers[-4].output) model.summary() training_features = model.predict(training_data) if len(filling_data) > 0: filling_features = model.predict(filling_data) else: r, c = training_features.shape filling_features = np.zeros((0, c)) testing_features = model.predict(testing_data) dict = { constants.training_suffix: (training_data, training_features, training_labels), constants.filling_suffix : (filling_data, filling_features, filling_labels), constants.testing_suffix : (testing_data, testing_features, testing_labels) } for suffix in dict: data_fn = constants.data_filename(data_prefix+suffix, n) features_fn = constants.data_filename(features_prefix+suffix, n) labels_fn = constants.data_filename(labels_prefix+suffix, n) d, f, l = dict[suffix] np.save(data_fn, d) np.save(features_fn, f) np.save(labels_fn, l) return histories
def obtain_features(model_prefix, features_prefix, labels_prefix, data_prefix, training_percentage, am_filling_percentage, experiment, occlusion=None, bars_type=None): """ Generate features for images. Uses the previously trained neural networks for generating the features corresponding to the images. It may introduce occlusions. """ (data, labels) = get_data(experiment, occlusion, bars_type) total = len(data) step = int(total / constants.training_stages) # Amount of data used for training the networks trdata = int(total * training_percentage) # Amount of data used for testing memories tedata = step n = 0 histories = [] for i in range(0, total, step): j = (i + tedata) % total if j > i: testing_data = data[i:j] testing_labels = labels[i:j] other_data = np.concatenate((data[0:i], data[j:total]), axis=0) other_labels = np.concatenate((labels[0:i], labels[j:total]), axis=0) training_data = other_data[:trdata] training_labels = other_labels[:trdata] filling_data = other_data[trdata:] filling_labels = other_labels[trdata:] else: testing_data = np.concatenate((data[0:j], data[i:total]), axis=0) testing_labels = np.concatenate((labels[0:j], labels[i:total]), axis=0) training_data = data[j:j + trdata] training_labels = labels[j:j + trdata] filling_data = data[j + trdata:i] filling_labels = labels[j + trdata:i] # Recreate the exact same model, including its weights and the optimizer model = tf.keras.models.load_model( constants.model_filename(model_prefix, n)) # Drop the autoencoder and the last layers of the full connected neural network part. classifier = Model(model.input, model.output[0]) no_hot = to_categorical(testing_labels) classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics='accuracy') history = classifier.evaluate(testing_data, no_hot, batch_size=batch_size, verbose=1, return_dict=True) print(history) histories.append(history) model = Model(classifier.input, classifier.layers[-4].output) model.summary() training_features = model.predict(training_data) if len(filling_data) > 0: filling_features = model.predict(filling_data) else: r, c = training_features.shape filling_features = np.zeros((0, c)) testing_features = model.predict(testing_data) dict = { constants.training_suffix: (training_data, training_features, training_labels), constants.filling_suffix: (filling_data, filling_features, filling_labels), constants.testing_suffix: (testing_data, testing_features, testing_labels) } for suffix in dict: data_fn = constants.data_filename(data_prefix + suffix, n) features_fn = constants.data_filename(features_prefix + suffix, n) labels_fn = constants.data_filename(labels_prefix + suffix, n) d, f, l = dict[suffix] np.save(data_fn, d) np.save(features_fn, f) np.save(labels_fn, l) n += 1 return histories
def train_networks(training_percentage, filename, experiment): stages = constants.training_stages (data, labels) = get_data(experiment, one_hot=True) total = len(data) step = total / stages # Amount of training data, from which a percentage is used for # validation. training_size = int(total * training_percentage) n = 0 histories = [] for k in range(stages): i = k * step j = int(i + training_size) % total i = int(i) if j > i: training_data = data[i:j] training_labels = labels[i:j] testing_data = np.concatenate((data[0:i], data[j:total]), axis=0) testing_labels = np.concatenate((labels[0:i], labels[j:total]), axis=0) else: training_data = np.concatenate((data[i:total], data[0:j]), axis=0) training_labels = np.concatenate((labels[i:total], labels[0:j]), axis=0) testing_data = data[j:i] testing_labels = labels[j:i] training_data, training_labels = expand_data(training_data, training_labels) truly_training = int(training_size * truly_training_percentage) validation_data = training_data[truly_training:] validation_labels = training_labels[truly_training:] training_data = training_data[:truly_training] training_labels = training_labels[:truly_training] input_img = Input(shape=(img_columns, img_rows, img_colors)) encoded = get_encoder(input_img) classified = get_classifier(encoded) decoded = get_decoder(encoded) model = Model(inputs=input_img, outputs=[classified, decoded]) model.compile(loss=['categorical_crossentropy', 'binary_crossentropy'], optimizer='adam', metrics='accuracy') model.summary() history = model.fit(training_data, (training_labels, training_data), batch_size=batch_size, epochs=epochs, validation_data=(validation_data, { 'classification': validation_labels, 'autoencoder': validation_data }), callbacks=[EarlyStoppingAtLossCrossing(patience)], verbose=2) histories.append(history) history = model.evaluate(testing_data, (testing_labels, testing_data), return_dict=True) histories.append(history) model.save(constants.model_filename(filename, n)) n += 1 return histories