def train_all(dataset_paths, epoch=100, batch_size=256): # Train all the models with corresponding datasets. # for dataset_path in dataset_paths: # print("Original Color") # model_resized = ConvAutoencoder() # train(model=model_resized, dataset_path=dataset_paths[0], epoch=epoch, batch_size=batch_size, model_name="original") print("2 Color") model_2 = ConvAutoencoder() train(model=model_2, dataset_path=dataset_paths[1], epoch=epoch, batch_size=batch_size, model_name="2")
def train_all(dataset_paths, epoch=10000, batch_size=256): # Train all the models with corresponding datasets. # for dataset_path in dataset_paths: # print("16 Color") # model_16 = ConvAutoencoder() # train(model=model_16, dataset_path=dataset_paths[0], epoch=epoch, batch_size=batch_size, model_name="16") # print("32 Color") # model_32 = ConvAutoencoder() # train(model=model_32, dataset_path=dataset_paths[1], epoch=epoch, batch_size=batch_size, model_name="32") # print("64 Color") # model_64 = ConvAutoencoder() # train(model=model_64, dataset_path=dataset_paths[2], epoch=epoch, batch_size=batch_size, model_name="64") print("128 Color") model_128 = ConvAutoencoder() train(model=model_128, dataset_path=dataset_paths[3], epoch=epoch, batch_size=batch_size, model_name="128")
str(total_test_loss / 3000) ]) f = open(root + '_results.csv', 'w') with f: writer = csv.writer(f) writer.writerows(result_list) return 0 total_time = t.time() # Train Original original_net = ConvAutoencoder() print(original_net) original_net = train(original_net, roots[0]) # Train 2 _2_net = ConvAutoencoder() result_list = train(_2_net, roots[1]) # # Train 4 _4_net = ConvAutoencoder() result_list = train(_4_net, roots[2]) # # Train 8 _8_net = ConvAutoencoder() result_list = train(_8_net, roots[3]) # # Train 16 _16_net = ConvAutoencoder() result_list = train(_16_net, roots[4])
import image_statistics import matplotlib.pyplot as plt import numpy as np import cv2 from sklearn.model_selection import train_test_split from PIL import Image import some_analysis processing_output = './processed/results_processing' data = image_processing.get_data_from_images(processing_output) mask = data[:, :, :, -1] != -1.0 #data = data[:, :, :, :-1] _, _, autoencoder = ConvAutoencoder.build(32, 48, 4, filters=(32, 64), latentDim=128) opt = Adam(lr=1e-3) #autoencoder.load_weights('autoencoder.h5') autoencoder.load_weights('autoencoder.h5') predictions = autoencoder.predict(data) errors = [] shape_errors = [] mask_original_mean = data[:, :, :, -1].mean() mask_pred_mean = predictions[:, :, :, -1].mean() stds = (data - predictions).std(axis=0) for i in range(mask.shape[0]): img = data[i][mask[i]] pred = predictions[i][mask[i]] mahalanobis_distance = (((img - pred) / stds[mask[i]])**2).sum()**0.5
for i in range(n_clusters): n_original = (preds_original == i).sum() n_generated = (preds_generated == i).sum() #n_images = n_original + n_generated #evaluation += n_generated * n_generated / n_images evaluation += abs(n_generated - n_original) evaluation /= n_original_images * 2 #evaluation = abs(evaluation - 0.5) * 2.0 print(evaluation) return evaluation if __name__ == '__main__': encoder, decoder, autoencoder = ConvAutoencoder.build(32, 48, 3, filters=(32, 64), latentDim=128) try: encoder.load_weights('encoder.h5') data_encoded = np.load('encoded_training_data.npy') except (ValueError, OSError): input_path = './bin' output_shape = (32, 48) processing_output = './processed/results_processing' data = image_processing.get_data_from_images(processing_output) data = data[:, :, :, :-1] autoencoder.compile(loss="mae", optimizer=Adam(lr=1e-3)) autoencoder.fit(data, data, epochs=100, batch_size=32) encoder.save('encoder.h5') decoder.save('decoder.h5')
print("[INFO] loading MNIST dataset...") ((trainX, _), (testX, _)) = mnist.load_data() # add a channel dimension to every image in the dataset, then scale # the pixel intensities to the range [0, 1] trainX = np.expand_dims(trainX, axis=-1) testX = np.expand_dims(testX, axis=-1) trainX = trainX.astype("float32") / 255.0 testX = testX.astype("float32") / 255.0 trainNoise = np.random.normal(loc=0.5, scale=0.5, size=trainX.shape) testNoise = np.random.normal(loc=0.5, scale=0.5, size=testX.shape) trainXNoisy = np.clip(trainX + trainNoise, 0, 1) testXNoisy = np.clip(testX + testNoise, 0, 1) # construct our convolutional autoencoder print("[INFO] building autoencoder...") (encoder, decoder, autoencoder) = ConvAutoencoder.build(28, 28, 1) opt = Adam(lr=1e-3) autoencoder.compile(loss="mse", optimizer=opt) # train the convolutional autoencoder H = autoencoder.fit(trainXNoisy, trainX, validation_data=(testXNoisy, testX), epochs=EPOCHS, batch_size=BS) # construct a plot that plots and saves the training history N = np.arange(0, EPOCHS) plt.style.use("ggplot") plt.figure() plt.plot(N, H.history["loss"], label="train_loss") plt.plot(N, H.history["val_loss"], label="val_loss")
import torch from autoencoder import ConvAutoencoder from utils import * model_encoder = ConvAutoencoder() # print(model_encoder) model_input = torch.randn(1, 3, 224, 224) from torchvision import datasets, transforms model_output = model_encoder.forward(model_input) model_features = model_encoder.extract_feature(model_input) print("output shape") print(model_output.shape) print("features shape") print(model_features.shape) print() preprocess_input = transforms.Compose([ transforms.Resize(224), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) ]) tensor2pil = transforms.ToPILImage() # device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") device = torch.device("cpu" if torch.cuda.is_available() else "cpu") import numpy as np import cv2
from sklearn.cluster import KMeans import image_processing import numpy as np import some_analysis from sklearn.manifold import TSNE import matplotlib.pyplot as plt from autoencoder import ConvAutoencoder input_path = './bin' output_shape = (32, 48) processing_output = './processed/results_processing' data = image_processing.get_data_from_images(processing_output) data = data[:, :, :, :-1] encoder, _, _ = ConvAutoencoder.build(32, 48, 3, filters=(32, 64), latentDim=512) encoder.load_weights('encoder.h5') data_encoded = encoder.predict(data) #data_reshaped = data.reshape((data.shape[0], -1)) n_clusters = 200 # Runs in parallel 4 CPUs kmeans = KMeans(n_clusters=n_clusters, n_init=15, n_jobs=8) # Train K-Means. y_pred_kmeans = kmeans.fit_predict(data_encoded) data += 1.0 data *= 127.5 array = np.empty((n_clusters), dtype=object)