def main(config): # Uncomment to see device placement # tf.debugging.set_log_device_placement(True) gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: try: # Currently, memory growth needs to be the same across GPUs for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) logical_gpus = tf.config.experimental.list_logical_devices('GPU') print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs") except RuntimeError as e: # Memory growth must be set before GPUs have been initialized print(e) # Load data on CPU with tf.device("/cpu:0"): data_loader = DataLoader(config) val_dataset = data_loader.load_val_dataset() # Make sure that everything is validated both on mr and kp loss config.use_mesh_repro_loss = True config.use_kp_loss = True trainer = Trainer(config, None, None, val_dataset, validation_only=True) trainer.validate_checkpoint()
def train_in_batches(data_size, clf=MLPClassifier(), batch_size=1000): # check if data_size is multiple of 100 if data_size % batch_size != 0: raise f"data_size must be multiple of {batch_size}, is {data_size}" dl = DL() classes = None for i in range(0, data_size, batch_size): # Load data data, labels = dl.load_data_batches(batch_size=batch_size, skip=i, return_1d=True) # Split with 0 test_size (only shuffle basically) X_train, _, y_train, _ = train_test_split(data, labels, test_size=1, shuffle=True) # Get classes from labels if classes is None: classes = np.sort(np.unique(labels)) clf.partial_fit(X_train, y_train, classes) # Load random test data and train test_data, test_labels = dl.load_random_test_data(return_1d=True) # print(np.array(test_data).shape, np.array(test_labels).shape) # print(clf.score(test_data, test_labels)) return clf
def main(config): # Uncomment to see device placement # tf.debugging.set_log_device_placement(True) gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: try: # Currently, memory growth needs to be the same across GPUs for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) logical_gpus = tf.config.experimental.list_logical_devices('GPU') print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs") except RuntimeError as e: # Memory growth must be set before GPUs have been initialized print(e) # Prepare directories for saving data prepare_dirs(config) # Load data on CPU with tf.device("/cpu:0"): data_loader = DataLoader(config) dataset = data_loader.load() smpl_loader = data_loader.get_smpl_loader() val_dataset = data_loader.load_val_dataset() trainer = Trainer(config, dataset, smpl_loader, val_dataset) save_config(config) trainer.train()
def __init__(self, **kwargs): self.parameters = kwargs self.logger = Logger(**kwargs) self.data_loader = DataLoader(**kwargs) self.model = GCN(input_dim=self.data_loader.get_input_feat_size(), hidden_dim=kwargs['hidden_dim'], num_classes=self.data_loader.get_num_classes(), dropout_prob=kwargs['dropout_prob'], bias=kwargs['bias']) self.optimizer = torch.optim.Adam(params=self.model.parameters(), lr=kwargs['lr']) self.cross_entropy = torch.nn.NLLLoss()
def main(config): prepare_dirs(config) # Load data on CPU with tf.device("/cpu:0"): data_loader = DataLoader(config) image_loader = data_loader.load() smpl_loader = data_loader.get_smpl_loader() trainer = HMRTrainer(config, image_loader, smpl_loader) save_config(config) trainer.train()
def test_should_generate_images_based_on_out_channels_parameter(): with pytest.raises(ValueError) as e: DataLoader(out_channels=0) assert "Out put channel should be either 3(RGB) or 1(Grey) but got 0" == str( e.value) # Should generate images with single channel channels = 1 data_loader = DataLoader(from_csv=True, target_labels=valid_target_labels, datapath=valid_csv_file_path, image_dimensions=valid_image_dimensions, csv_label_col=csv_label_col, csv_image_col=csv_image_col, out_channels=channels) images, labels = data_loader.load_data() assert list(images.shape[1:]) == list(valid_image_dimensions) + [channels] # Should generate images with 3 channel channels = 3 data_loader = DataLoader(from_csv=True, target_labels=valid_target_labels, datapath=valid_csv_file_path, image_dimensions=valid_image_dimensions, csv_label_col=csv_label_col, csv_image_col=csv_image_col, out_channels=channels) images, labels = data_loader.load_data() assert list(images.shape[1:]) == list(valid_image_dimensions) + [channels]
def _main(args): # load input data data_loader = DataLoader(args.path) df_prod = data_loader.df_previous_production df_constrains = data_loader.df_constrains scrap_prices = data_loader.scrap_prices scrap_inventory = data_loader.scrap_inventory prod_schedule = data_loader.production_schedule # fit estimators cu_estimator = CuEstimator() cu_estimator.fit(df_prod) yield_estimator = YieldEstimator() yield_estimator.fit(df_prod) optimizer = RecipeOptimizer(cu_estimator=cu_estimator, yield_estimator=yield_estimator, df_constrains=df_constrains, prices=scrap_prices, cu_target_gap=args.cu_factor, heat_weight_gap=args.mass_factor) results = [] for idx, row in prod_schedule.iterrows(): recipe = optimizer.optimize(row, scrap_inventory) # update scrap on hand for scrap_type, value in recipe.items(): scrap_inventory[scrap_type] = max( 0, scrap_inventory[scrap_type] - value) recipe_with_steel_grade = dict(recipe) recipe_with_steel_grade.update({'steel_grade': row['steel_grade']}) results.append({ 'heat_seq': row['heat_seq'], 'heat_id': row['heat_id'], 'steel_grade': row['steel_grade'], 'predicted_tap_weight': yield_estimator.predict(recipe_with_steel_grade), 'predicted_chemistry': { 'cu_pct': cu_estimator.predict(recipe_with_steel_grade) }, 'suggested_recipe': recipe }) print(json.dumps(results))
def train(args): data_loader = DataLoader(args.train_file, args.dev_file, args.src_suffix, args.trg_suffix, args.w2i_map_file, args.batch_size, args.pool_size, pad=0) src_vocab_size, trg_vocab_size = data_loader.src_vocab_size, data_loader.trg_vocab_size model = make_model(src_vocab_size=src_vocab_size, trg_vocab_size=trg_vocab_size) model.to(device) criterion = LabelSmoothing(smoothing=0.1, vocab_size=trg_vocab_size, pad_idx=0, device=device) optimizer = NoamOpt(model.parameters(), d_model=args.d_model, warmup=args.warmup, factor=args.factor) # best_loss = float('inf') for ep in range(1000): model.train() train_iter = data_loader.create_batches("train") run_epoch(ep, train_iter, model, criterion, optimizer) if (ep + 1) % EPOCH_CHECK == 0: with torch.no_grad(): model.eval() dev_iter = data_loader.create_batches("dev") print("===============eval===============") run_epoch(ep, dev_iter, model, criterion, optimizer=None) # if dev_loss < best_loss: # best_loss = dev_loss save_model(model, args.model_path + "_" + str(ep) + ".tar") print("===============eval===============")
def main(): print(tf.__version__) args = parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' deprecation._PRINT_DEPRECATION_WARNINGS = False # tf.logging.set_verbosity(tf.logging.INFO) # load data loader = DataLoader(args.training_data_dir) # train train((loader.G_trn, loader.features, loader.walks, loader.edge_text, len(loader.vocab)), args)
def main(config): show_random = False # Load data on CPU with tf.device("/cpu:0"): data_loader = DataLoader(config) dataset = data_loader.load() smpl_dataset = data_loader.get_smpl_loader() if show_random: dataset = dataset.shuffle(buffer_size=10000) dataset = dataset.batch(config.batch_size) for image, seg_gt, kps in dataset: f, axarr = plt.subplots(config.batch_size, 2, figsize=(2, config.batch_size), dpi=224) plt.subplots_adjust(left=0.0, bottom=0.0, right=1.0, top=1.0, wspace=0.0, hspace=0.0) for j in range(config.batch_size): # undo preprocessing img = (image[j] + 1) * 0.5 ks = ((kps[j, :, :2] + 1) * 0.5) * config.img_size # only take first 14 keypoints (no face keyoints) for visualization ks = ks[:14] # segmentation is stored with only 1 channel but needs 3 channels for vis. seg = tf.concat([seg_gt[j], seg_gt[j], seg_gt[j]], axis=2) if config.batch_size == 1: # draw image axarr[0].imshow(img) axarr[0].axis('off') axarr[1].scatter(x=ks[:, 0], y=ks[:, 1], c=[ 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'b', 'g', 'r', 'y' ], s=2) axarr[1].imshow(seg) axarr[1].axis('off') else: # draw image axarr[j, 0].imshow(img) axarr[j, 0].axis('off') axarr[j, 1].scatter(x=ks[:, 0], y=ks[:, 1], c=[ 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'b', 'g', 'r', 'y' ], s=2) axarr[j, 1].imshow(seg) axarr[j, 1].axis('off') plt.show()
def __init__(self, dataset_name, upscale_power_factor, n_residual_blocks, local_path=None): # Input shape self.channels = 1 self.hr_height = 512 # High resolution height self.hr_width = 512 # High resolution width self.checkpoint_path = 'checkpoints/' assert isinstance(upscale_power_factor, int), "upscale power factor must be int!" self.upscale_power_factor = upscale_power_factor self.upscale_factor = 2**self.upscale_power_factor self.lr_height = int(self.hr_height / self.upscale_factor) # Low resolution height self.lr_width = int(self.hr_width / self.upscale_factor) # Low resolution width self.hr_shape = (self.hr_height, self.hr_width, self.channels) self.lr_shape = (self.lr_height, self.lr_width, self.channels) # Number of residual blocks in the generator self.n_residual_blocks = n_residual_blocks #16 self.optimizer = Adam(0.0002, 0.5) # We use a pre-trained VGG19 model to extract image features from the high resolution # and the generated high resolution images and minimize the mse between them self.vgg = self.build_vgg() self.vgg.trainable = False self.vgg.compile(loss='mse', optimizer=self.optimizer, metrics=['accuracy']) # Configure data loader self.dataset_name = dataset_name #'img_sst' self.data_loader = DataLoader(dataset_name=self.dataset_name, img_res=(self.hr_height, self.hr_width), local_path=local_path) # Calculate output shape of D (PatchGAN) patch = int(self.hr_height / 2**4) self.disc_patch = (patch, patch, 1) # Number of filters in the first layer of G and D self.gf = 64 self.df = 64 # Build and compile the discriminator self.discriminator = self.build_discriminator() self.discriminator.compile(loss='mse', optimizer=self.optimizer, metrics=['accuracy']) # Build the generator self.generator = self.build_generator() # High res. and low res. images img_hr = Input(shape=self.hr_shape) img_lr = Input(shape=self.lr_shape) # Generate high res. version from low res. fake_hr = self.generator(img_lr) # Extract image features of the generated img fake_hr_temp = Concatenate(axis=-1)([fake_hr, fake_hr]) fake_hr_temp = Concatenate(axis=-1)([fake_hr_temp, fake_hr]) #fake_hr_temp = K.tile(fake_hr, (1, 1, 1, 3)) fake_features = self.vgg(fake_hr_temp) # For the combined model we will only train the generator self.discriminator.trainable = False # Discriminator determines validity of generated high res. images validity = self.discriminator(fake_hr) self.combined = Model([img_lr, img_hr], [validity, fake_features]) self.combined.compile(loss=['binary_crossentropy', 'mse'], loss_weights=[1e-3, 1], optimizer=self.optimizer)
class SRGAN(): def __init__(self, dataset_name, upscale_power_factor, n_residual_blocks, local_path=None): # Input shape self.channels = 1 self.hr_height = 512 # High resolution height self.hr_width = 512 # High resolution width self.checkpoint_path = 'checkpoints/' assert isinstance(upscale_power_factor, int), "upscale power factor must be int!" self.upscale_power_factor = upscale_power_factor self.upscale_factor = 2**self.upscale_power_factor self.lr_height = int(self.hr_height / self.upscale_factor) # Low resolution height self.lr_width = int(self.hr_width / self.upscale_factor) # Low resolution width self.hr_shape = (self.hr_height, self.hr_width, self.channels) self.lr_shape = (self.lr_height, self.lr_width, self.channels) # Number of residual blocks in the generator self.n_residual_blocks = n_residual_blocks #16 self.optimizer = Adam(0.0002, 0.5) # We use a pre-trained VGG19 model to extract image features from the high resolution # and the generated high resolution images and minimize the mse between them self.vgg = self.build_vgg() self.vgg.trainable = False self.vgg.compile(loss='mse', optimizer=self.optimizer, metrics=['accuracy']) # Configure data loader self.dataset_name = dataset_name #'img_sst' self.data_loader = DataLoader(dataset_name=self.dataset_name, img_res=(self.hr_height, self.hr_width), local_path=local_path) # Calculate output shape of D (PatchGAN) patch = int(self.hr_height / 2**4) self.disc_patch = (patch, patch, 1) # Number of filters in the first layer of G and D self.gf = 64 self.df = 64 # Build and compile the discriminator self.discriminator = self.build_discriminator() self.discriminator.compile(loss='mse', optimizer=self.optimizer, metrics=['accuracy']) # Build the generator self.generator = self.build_generator() # High res. and low res. images img_hr = Input(shape=self.hr_shape) img_lr = Input(shape=self.lr_shape) # Generate high res. version from low res. fake_hr = self.generator(img_lr) # Extract image features of the generated img fake_hr_temp = Concatenate(axis=-1)([fake_hr, fake_hr]) fake_hr_temp = Concatenate(axis=-1)([fake_hr_temp, fake_hr]) #fake_hr_temp = K.tile(fake_hr, (1, 1, 1, 3)) fake_features = self.vgg(fake_hr_temp) # For the combined model we will only train the generator self.discriminator.trainable = False # Discriminator determines validity of generated high res. images validity = self.discriminator(fake_hr) self.combined = Model([img_lr, img_hr], [validity, fake_features]) self.combined.compile(loss=['binary_crossentropy', 'mse'], loss_weights=[1e-3, 1], optimizer=self.optimizer) def build_vgg(self): """ Builds a pre-trained VGG19 model that outputs image features extracted at the third block of the model """ vgg = VGG19(weights="imagenet") # Set outputs to outputs of last conv. layer in block 3 # See architecture at: https://github.com/keras-team/keras/blob/master/keras/applications/vgg19.py vgg.outputs = [vgg.layers[9].output] #img = Input(shape=self.hr_shape) img = Input(shape=(self.hr_height, self.hr_width, 3)) # Extract image features img_features = vgg(img) return Model(img, img_features) def build_generator(self): def residual_block(layer_input, filters): """Residual block described in paper""" d = Conv2D(filters, kernel_size=3, strides=1, padding='same')(layer_input) d = Activation('relu')(d) d = BatchNormalization(momentum=0.8)(d) d = Conv2D(filters, kernel_size=3, strides=1, padding='same')(d) d = BatchNormalization(momentum=0.8)(d) d = Add()([d, layer_input]) return d def deconv2d(layer_input): """Layers used during upsampling""" u = UpSampling2D(size=2)(layer_input) u = Conv2D(256, kernel_size=3, strides=1, padding='same')(u) u = Activation('relu')(u) return u # Low resolution image input img_lr = Input(shape=self.lr_shape) # Pre-residual block c1 = Conv2D(64, kernel_size=9, strides=1, padding='same')(img_lr) c1 = Activation('relu')(c1) # Propogate through residual blocks r = residual_block(c1, self.gf) for _ in range(self.n_residual_blocks - 1): r = residual_block(r, self.gf) # Post-residual block c2 = Conv2D(64, kernel_size=3, strides=1, padding='same')(r) c2 = BatchNormalization(momentum=0.8)(c2) c2 = Add()([c2, c1]) # Upsampling u = c2 for upx in range(self.upscale_power_factor): u = deconv2d(u) # Generate high resolution output gen_hr = Conv2D(self.channels, kernel_size=9, strides=1, padding='same', activation='tanh')(u) return Model(img_lr, gen_hr) def build_discriminator(self): def d_block(layer_input, filters, strides=1, bn=True): """Discriminator layer""" d = Conv2D(filters, kernel_size=3, strides=strides, padding='same')(layer_input) d = LeakyReLU(alpha=0.2)(d) if bn: d = BatchNormalization(momentum=0.8)(d) return d # Input img d0 = Input(shape=self.hr_shape) d1 = d_block(d0, self.df, bn=False) d2 = d_block(d1, self.df, strides=2) d3 = d_block(d2, self.df * 2) d4 = d_block(d3, self.df * 2, strides=2) d5 = d_block(d4, self.df * 4) d6 = d_block(d5, self.df * 4, strides=2) d7 = d_block(d6, self.df * 8) d8 = d_block(d7, self.df * 8, strides=2) d9 = Dense(self.df * 16)(d8) d10 = LeakyReLU(alpha=0.2)(d9) validity = Dense(1, activation='sigmoid')(d10) return Model(d0, validity) def train(self, epochs, sample_rslt_dir, batch_size=1, sample_interval=1, save_interval=1, load_checkpoint=False, checkpoint_id=0): start_time = datetime.datetime.now() N = 100 start = 0 if load_checkpoint: print('Models Loading ...') self.discriminator = load_model(self.checkpoint_path + str(checkpoint_id) + '-discriminator.h5') self.generator = load_model(self.checkpoint_path + str(checkpoint_id) + '-generator.h5') # High res. and low res. images img_hr = Input(shape=self.hr_shape) img_lr = Input(shape=self.lr_shape) # Generate high res. version from low res. fake_hr = self.generator(img_lr) # Extract image features of the generated img fake_hr_temp = Concatenate(axis=-1)([fake_hr, fake_hr]) fake_hr_temp = Concatenate(axis=-1)([fake_hr_temp, fake_hr]) #fake_hr_temp = K.tile(fake_hr, (1, 1, 1, 3)) fake_features = self.vgg(fake_hr_temp) # For the combined model we will only train the generator self.discriminator.trainable = False # Discriminator determines validity of generated high res. images validity = self.discriminator(fake_hr) self.combined = Model([img_lr, img_hr], [validity, fake_features]) self.combined.compile(loss=['binary_crossentropy', 'mse'], loss_weights=[1e-3, 1], optimizer=self.optimizer) start = checkpoint_id + 1 print('Models Loaded Successfully! ') for epoch in range(start, epochs): for b_id in range(int(N / batch_size) + 1): # ---------------------- # Train Discriminator # ---------------------- # Sample images and their conditioning counterparts imgs_hr, imgs_lr = self.data_loader.load_data(batch_size) imgs_hr = np.expand_dims(imgs_hr, axis=3) imgs_lr = np.expand_dims(imgs_lr, axis=3) # From low res. image generate high res. version fake_hr = self.generator.predict(imgs_lr) valid = np.ones((batch_size, ) + self.disc_patch) fake = np.zeros((batch_size, ) + self.disc_patch) # Train the discriminators (original images = real / generated = Fake) d_loss_real = self.discriminator.train_on_batch(imgs_hr, valid) d_loss_fake = self.discriminator.train_on_batch(fake_hr, fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) # ------------------ # Train Generator # ------------------ # Sample images and their conditioning counterparts imgs_hr, imgs_lr = self.data_loader.load_data(batch_size) imgs_hr = np.expand_dims(imgs_hr, axis=3) imgs_lr = np.expand_dims(imgs_lr, axis=3) # The generators want the discriminators to label the generated images as real valid = np.ones((batch_size, ) + self.disc_patch) # print(imgs_hr) # Extract ground truth image features using pre-trained VGG19 model imgs_hr_temp = np.repeat(imgs_hr, 3, axis=-1) image_features = self.vgg.predict(imgs_hr_temp) # Train the generators g_loss = self.combined.train_on_batch([imgs_lr, imgs_hr], [valid, image_features]) elapsed_time = datetime.datetime.now() - start_time # Plot the progress print("epoch %d batch %d time: %s" % (epoch, b_id, elapsed_time)) if epoch % sample_interval == 0: self.sample_images(epoch, sample_rslt_dir) if epoch % save_interval == 0: print('Saving Models ...') self.discriminator.save(self.checkpoint_path + str(epoch) + '-discriminator.h5') self.generator.save(self.checkpoint_path + str(epoch) + '-generator.h5') #self.combined.save(self.checkpoint_path + str(epoch) + '-combined.h5') print('Models Saved Successfully!') def sample_images(self, epoch, sample_rslt_dir): if not os.path.exists(sample_rslt_dir): os.makedirs(sample_rslt_dir) #os.makedirs('images/%s' % self.dataset_dir, exist_ok=True) n_rows, n_cols = 1, 2 imgs_hr, imgs_lr = self.data_loader.load_data(batch_size=n_rows, is_testing=True) vmin = np.zeros((n_rows, )) vmax = np.zeros((n_rows, )) for i in range(n_rows): vmin[i] = imgs_hr[i].min() vmax[i] = imgs_hr[i].max() imgs_hr = np.expand_dims(imgs_hr, axis=3) imgs_lr = np.expand_dims(imgs_lr, axis=3) fake_hr = self.generator.predict(imgs_lr) # Rescale images 0 - 1 imgs_lr = 0.5 * imgs_lr + 0.5 fake_hr = 0.5 * fake_hr + 0.5 imgs_hr = 0.5 * imgs_hr + 0.5 # Save generated images and the high resolution originals titles = ['Generated', 'Original'] fig, axs = plt.subplots(n_rows, n_cols) cnt = 0 for row in range(n_rows): for col, image in enumerate([fake_hr, imgs_hr]): axs[row, col].imshow(np.squeeze(image[row], axis=2), vmin=vmin[row], vmax=vmax[row]) axs[row, col].set_title(titles[col]) axs[row, col].axis('off') cnt += 1 fig.savefig(sample_rslt_dir + "/{}.png".format(epoch)) #fig.savefig("images/%s/%d.png" % (self.dataset_dir, epoch)) plt.close() # Save low resolution images for comparison for i in range(n_rows): fig = plt.figure() plt.imshow(np.squeeze(imgs_lr[i], axis=2), vmin=vmin[i], vmax=vmax[i]) fig.savefig(sample_rslt_dir + "/{}_lowers_{}.png".format(epoch, i)) #fig.savefig('images/%s/%d_lowres%d.png' % (self.dataset_dir, epoch, i)) plt.close()
# Setup import os import sys root_dir = os.path.join(os.getcwd(), '..') sys.path.append('e:\\Projekte\\2021\\ML\\doodle-classifier') sys.path.append(root_dir) # print(os.getcwd()) import numpy as np from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split from src.data_loader import DataLoader as DL from src.coach import train_in_batches dl = DL() clf_batches = MLPClassifier(hidden_layer_sizes=(128, )) for i in range(3): data, labels = dl.get_next_training_set(100) clf_batches.partial_fit(data, labels, np.unique(labels)) # import os # import sys # root_dir = os.path.join(os.getcwd(), '..') # sys.path.append(root_dir) # import numpy as np # import png # from PIL import Image, ImageDraw # img = [[[203,193,177,143,104,66,34,17,12,14,30,53,75,100,125,152,177,204,228,245,255,258,259,258,251,239,228,211,199,190,180,171,163,163],[51,45,45,52,74,106,148,192,233,264,288,305,313,315,315,303,278,244,207,174,143,117,92,75,61,48,40,35,33,33,33,34,42,42],[0,27,45,58,78,94,111,127,143,161,177,194,210,228,244,261,277,294,310,328,344,361,380,397,413,429,449,463,481,496,512,529,546,566]],[[139,137,137,134,126,116,109,109,111,118,121,123,124,123],[56,61,73,97,133,179,226,266,292,312,325,334,340,338],[965,994,1011,1027,1043,1061,1081,1098,1115,1130,1148,1166,1194,1232]],[[21,23,31,45,67,95,128,161,191,220,252,273,294,306,306],[199,206,210,212,212,204,190,179,173,167,157,151,146,144,144],[1399,1443,1462,1477,1494,1510,1527,1543,1561,1580,1599,1614,1628,1642,1663]],[[216,205,188,158,125,96,75,63,58,56,55,55,55,56],[60,71,93,128,175,222,266,299,320,337,348,357,364,363],[2049,2093,2110,2126,2143,2162,2177,2195,2220,2230,2247,2261,2277,2362]],[[53,60,69,85,108,134,162,188,207,223,234,243,243],[136,143,151,163,179,195,212,227,238,250,259,268,268],[2535,2561,2577,2594,2611,2630,2644,2660,2676,2699,2715,2731,2749]]]
def test_load_directory_data(): invalid_directory_path = 'invalid_directory_path' valid_dummy_directory = './resources/dummy_data_directory' empty_dummy_directory = './resources/dummy_empty_data_directory' channels = 1 # should raise error when receives an invalid directory path with pytest.raises(NotADirectoryError): DataLoader(from_csv=False, datapath=invalid_directory_path) # should raise error when tries to load empty directory data_loader = DataLoader(from_csv=False, datapath=empty_dummy_directory) with pytest.raises(AssertionError): data_loader.load_data() # should assign an image's parent directory name as its label data_loader = DataLoader(from_csv=False, datapath=valid_dummy_directory) images, labels, label_index_map = data_loader.load_data() label_count = len(label_index_map.keys()) label = [0] * label_count label[label_index_map['happiness']] = 1 assert label == labels[0] data_loader = DataLoader(from_csv=False, datapath=valid_dummy_directory) images, labels, label_index_map = data_loader.load_data() # should return non-empty image and label arrays when given valid arguments assert len(images) > 0 and len(labels) > 0 # should return same number of labels and images when given valid arguments assert len(images) == len(labels) # should reshape image to contain channel_axis in channel_last format assert images.shape[-1] == channels
class Model(layers.Layer): def __init__(self, hyperparameters): super(Model, self).__init__() self.auxiliary_data_source = hyperparameters['auxiliary_data_source'] self.all_data_sources = ['resnet_features', self.auxiliary_data_source] self.DATASET = hyperparameters['dataset'] self.num_shots = hyperparameters['num_shots'] self.latent_size = hyperparameters['latent_size'] self.batch_size = hyperparameters['batch_size'] self.hidden_size_rule = hyperparameters['hidden_size_rule'] self.warm_up = hyperparameters['model_specifics']['warm_up'] self.generalized = hyperparameters['generalized'] self.classifier_batch_size = 32 self.img_seen_samples = hyperparameters['samples_per_class'][self.DATASET][0] self.att_seen_samples = hyperparameters['samples_per_class'][self.DATASET][1] self.att_unseen_samples = hyperparameters['samples_per_class'][self.DATASET][2] self.img_unseen_samples = hyperparameters['samples_per_class'][self.DATASET][3] self.recog_loss_function = hyperparameters['loss'] self.num_epoch = hyperparameters['epochs'] self.lr_cls = hyperparameters['lr_cls'] self.cross_reconstruction = hyperparameters['model_specifics']['cross_reconstruction'] self.cls_train_epochs = hyperparameters['cls_train_steps'] self.dataset = DataLoader(self.DATASET, copy.deepcopy(self.auxiliary_data_source)) self.reparameterize_with_noise = False self.current_epoch = 0 if self.DATASET == 'CUB': self.num_classes = 200 self.num_novel_classes = 50 elif self.DATASET == 'SUN': self.num_classes = 717 self.num_novel_classes = 72 elif self.DATASET == 'AWA1' or self.DATASET == 'AWA2': self.num_classes = 50 self.num_novel_classes = 10 feature_dimensions = [2048, self.dataset.aux_data.size(1)] # Here, the encoders and decoders for all modalities are created and put into dict self.encoder = {} for datatype, dim in zip(self.all_data_sources,feature_dimensions): self.encoder[datatype] = models.encoder_template(dim, self.latent_size, self.hidden_size_rule[datatype]) print(str(datatype) + ' ' + str(dim)) self.decoder = {} for datatype, dim in zip(self.all_data_sources, feature_dimensions): self.decoder[datatype] = models.decoder_template(self.latent_size, dim, self.hidden_size_rule[datatype]) # An optimizer for all encoders and decoders is defined here self.parameters_to_optimize = list(self.parameters()) for datatype in self.all_data_sources: self.parameters_to_optimize += list(self.encoder[datatype].parameters()) self.parameters_to_optimize += list(self.decoder[datatype].parameters()) self.optimizer = tf.optimizers.Adam(learning_rate=hyperparameters['lr_gen_model'], beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0, amsgrad=True) if self.recog_loss_function == 'l2': # size_average=False self.reconstruction_criterion = tf.keras.losses.MSE() elif self.recog_loss_function == 'l1': # size_average=False self.reconstruction_criterion = tf.keras.losses.MAE() def reparameterize(self, mu, log_var): if self.reparameterize_with_noise: sigma = tf.math.exp(log_var) eps = tf.random.normal(sigma.shape, mean=0.0, stddev=1.0) return mu + sigma * eps else: return mu def forward(self): pass def train_step(self, img, attr): # -------------------------------------------------------------------------------------------------------------- # scale the loss terms according to the warm_up schedule tmp1 = 1.0 * (self.current_epoch - self.warm_up['cross_reconstruction']['start_epoch']) tmp2 = 1.0 * (self.warm_up['cross_reconstruction']['end_epoch'] - self.warm_up['cross_reconstruction']['start_epoch']) f1 = tmp1 / tmp2 f1 = f1 * (1.0 * self.warm_up['cross_reconstruction']['factor']) cross_reconstruction_factor = tf.zeros(min(max(f1, 0), self.warm_up['cross_reconstruction']['factor']), dtype=tf.float32) tmp1 = 1.0 * (self.current_epoch - self.warm_up['beta']['start_epoch']) tmp2 = 1.0 * (self.warm_up['beta']['end_epoch'] - self.warm_up['beta']['start_epoch']) f2 = tmp1 / tmp2 f2 = f2 * (1.0 * self.warm_up['beta']['factor']) beta = tf.zeros(min(max(f2, 0), self.warm_up['beta']['factor']), dtype=tf.float32) tmp1 = 1.0 * (self.current_epoch - self.warm_up['distance']['start_epoch']) tmp2 = 1.0 * (self.warm_up['distance']['end_epoch'] - self.warm_up['distance']['start_epoch']) f3 = tmp1 / tmp2 f3 = f3 * (1.0 * self.warm_up['distance']['factor']) distance_factor = tf.zeros(min(max(f3, 0), self.warm_up['distance']['factor'])) with tf.GradientTape() as tape: # ---------------------------------------------------------------------------------------------------------- # Encode image features and additional features mu_img, log_var_img = self.encoder['resnet_features'](img) z_from_img = self.reparameterize(mu_img, log_var_img) mu_att, log_var_att = self.encoder[self.auxiliary_data_source](attr) z_from_att = self.reparameterize(mu_att, log_var_att) # ---------------------------------------------------------------------------------------------------------- # Reconstruct inputs img_from_img = self.decoder['resnet_features'](z_from_img) att_from_att = self.decoder[self.auxiliary_data_source](z_from_att) reconstruction_loss = self.reconstruction_criterion(img_from_img, img) + \ self.reconstruction_criterion(att_from_att, attr) # ---------------------------------------------------------------------------------------------------------- # Cross Reconstruction Loss img_from_att = self.decoder['resnet_features'](z_from_att) att_from_img = self.decoder[self.auxiliary_data_source](z_from_img) cross_reconstruction_loss = self.reconstruction_criterion(img_from_att, img) + \ self.reconstruction_criterion(att_from_img, attr) # ---------------------------------------------------------------------------------------------------------- # KL-Divergence kld = (0.5 * tf.reduce_sum(1 + log_var_att - mu_att.pow(2) - log_var_att.exp())) + \ (0.5 * tf.reduce_sum(1 + log_var_img - mu_img.pow(2) - log_var_img.exp())) # ---------------------------------------------------------------------------------------------------------- # Distribution Alignment tmp1 = tf.reduce_sum((mu_img - mu_att) ** 2, dim=1) tmp2 = tf.reduce_sum((tf.math.sqrt(log_var_img.exp()) - tf.math.sqrt(log_var_att.exp())) ** 2, dim=1) distance = tf.reduce_sum(tf.sqrt(tmp1 + tmp2)) # ---------------------------------------------------------------------------------------------------------- # Put the loss together and call the optimizer loss = reconstruction_loss - beta * kld if cross_reconstruction_loss > 0: loss += cross_reconstruction_factor * cross_reconstruction_loss if distance_factor > 0: loss += distance_factor * distance gradients = tape.gradient(loss, self.parameters_to_optimize) self.optimizer.apply_gradients(zip(gradients, self.parameters_to_optimize)) return loss def train_vae(self): losses = [] # leave both statements self.train() self.reparameterize_with_noise = True print('train for reconstruction') for epoch in range(0, self.num_epoch): self.current_epoch = epoch i = -1 for iters in range(0, self.dataset.ntrain, self.batch_size): i += 1 label, data_from_modalities = self.dataset.next_batch(self.batch_size) loss = self.train_step(data_from_modalities[0], data_from_modalities[1]) if i % 50 == 0: print('epoch %d | %d \t | loss = %f' % (epoch, i, loss)) if i > 0: losses.append(loss) # turn into evaluation mode: for key, value in self.encoder.items(): self.encoder[key].eval() for key, value in self.decoder.items(): self.decoder[key].eval() return losses def train_classifier(self): if self.num_shots > 0 : print('================ transfer features from test to train ==================') self.dataset.transfer_features(self.num_shots, num_queries='num_features') history = [] # stores accuracies cls_seen_classes = self.dataset.seen_classes cls_novel_classes = self.dataset.novel_classes train_seen_feat = self.dataset.data['train_seen']['resnet_features'] train_seen_label = self.dataset.data['train_seen']['labels'] novel_class_aux_data = self.dataset.novel_class_aux_data seen_class_aux_data = self.dataset.seen_class_aux_data novel_corresponding_labels = self.dataset.novel_classes.long().to(self.device) seen_corresponding_labels = self.dataset.seen_classes.long().to(self.device) # The resnet_features for testing the classifier are loaded here novel_test_feat = self.dataset.data['test_unseen']['resnet_features'] seen_test_feat = self.dataset.data['test_seen']['resnet_features'] test_seen_label = self.dataset.data['test_seen']['labels'] test_novel_label = self.dataset.data['test_unseen']['labels'] train_unseen_feat = self.dataset.data['train_unseen']['resnet_features'] train_unseen_label = self.dataset.data['train_unseen']['labels'] # in ZSL mode: if not self.generalized: # there are only 50 classes in ZSL (for CUB) # novel_corresponding_labels = list of all novel classes (as tensor) # test_novel_label = mapped to 0-49 in classifier function # those are used as targets, they have to be mapped to 0-49 right here: novel_corresponding_labels = map_label(novel_corresponding_labels, novel_corresponding_labels) if self.num_shots > 0: # not generalized and at least 1 shot means normal FSL setting (use only unseen classes) train_unseen_label = map_label(train_unseen_label, cls_novel_classes) # for FSL, we train_seen contains the unseen class examples # for ZSL, train seen label is not used # if self.num_shots>0: # train_seen_label = map_label(train_seen_label,cls_novel_classes) test_novel_label = map_label(test_novel_label, cls_novel_classes) # map cls novel_classes last cls_novel_classes = map_label(cls_novel_classes, cls_novel_classes) if self.generalized: print('mode: gzsl') clf = LinearLogSoftMax(self.latent_size, self.num_classes) else: print('mode: zsl') clf = LinearLogSoftMax(self.latent_size, self.num_novel_classes) clf.apply(models.weights_init) # -------------------------------------------------------------------------------------------------------------- # Model Inference # # -------------------------------------------------------------------------------------------------------------# # Preparing the test set: # convert raw test data into z vectors self.reparameterize_with_noise = False mu1, var1 = self.encoder['resnet_features'](novel_test_feat) test_novel_x = self.reparameterize(mu1, var1).to(self.device).data test_novel_y = test_novel_label.to(self.device) mu2, var2 = self.encoder['resnet_features'](seen_test_feat) test_seen_x = self.reparameterize(mu2, var2).to(self.device).data test_seen_y = test_seen_label.to(self.device) # -------------------------------------------------------------------------------------------------------------# # Preparing the train set: # chose n random image features per class. If n exceeds the number of image features per class, duplicate # some. Next, convert them to latent z features. self.reparameterize_with_noise = True def sample_train_data(features, label, sample_per_class): """ Sample_train_data_on_sample_per_class_basis :param features: :param label: :param sample_per_class: :return: """ sample_per_class = int(sample_per_class) if sample_per_class != 0 and len(label) != 0: classes = label.unique() features_to_return = 0 labels_to_return = 0 for i, s in enumerate(classes): # order of features and labels must coincide features_of_that_class = features[label == s, :] # if number of selected features is smaller than the number of features we want per class: tmp = max(1, sample_per_class / features_of_that_class.size(0)) multiplier = tf.cast(tf.math.ceil(tmp), dtype=tf.int32) features_of_that_class = features_of_that_class.repeat(multiplier, 1) if i == 0: features_to_return = features_of_that_class[:sample_per_class, :] labels_to_return = s.repeat(sample_per_class) else: features_to_return = tf.concat([features_to_return, features_of_that_class[:sample_per_class, :]], dim=0) labels_to_return = tf.concat([labels_to_return, s.repeat(sample_per_class)], dim=0) return features_to_return, labels_to_return else: return None, None # Some of the following might be empty tensors if the specified number of samples is zero : img_seen_feat, img_seen_label = sample_train_data(train_seen_feat, train_seen_label, self.img_seen_samples) img_unseen_feat, img_unseen_label = sample_train_data(train_unseen_feat, train_unseen_label, self.img_unseen_samples) att_unseen_feat, att_unseen_label = sample_train_data(novel_class_aux_data, novel_corresponding_labels, self.att_unseen_samples ) att_seen_feat, att_seen_label = sample_train_data(seen_class_aux_data, seen_corresponding_labels, self.att_seen_samples) def convert_datapoints_to_z(features, encoder): if features.size(0) != 0: mu_, log_var_ = encoder(features) return self.reparameterize(mu_, log_var_) else: return None z_seen_img = convert_datapoints_to_z(img_seen_feat, self.encoder['resnet_features']) z_unseen_img = convert_datapoints_to_z(img_unseen_feat, self.encoder['resnet_features']) z_seen_att = convert_datapoints_to_z(att_seen_feat, self.encoder[self.auxiliary_data_source]) z_unseen_att = convert_datapoints_to_z(att_unseen_feat, self.encoder[self.auxiliary_data_source]) train_z = [z_seen_img, z_unseen_img, z_seen_att, z_unseen_att] train_l = [img_seen_label, img_unseen_label, att_seen_label, att_unseen_label] # empty tensors are sorted out train_x = [train_z[i] for i in range(len(train_z)) if train_z[i].size(0) != 0] train_y = [train_l[i] for i in range(len(train_l)) if train_z[i].size(0) != 0] # -------------------------------------------------------------------------------------------------------------# # Initializing the classifier and train one epoch cls = classifier.Classifier(clf, train_x, train_y, test_seen_x, test_seen_y, test_novel_x, test_novel_y, cls_seen_classes, cls_novel_classes, self.num_classes, self.device, self.lr_cls, 0.5, 1, self.classifier_batch_size, self.generalized) for k in range(self.cls_train_epochs): if k > 0: if self.generalized: cls.acc_seen, cls.acc_novel, cls.H = cls.fit(train_x, train_y, test_seen_x, test_seen_y, cls_seen_classes, test_novel_x, test_novel_y, cls_novel_classes) else: cls.acc = cls.fit_zsl(train_x, train_y, test_novel_x, test_novel_y, cls_novel_classes) if self.generalized: print('[%.1f] novel=%.4f, seen=%.4f, h=%.4f, loss=%.4f' % (k, cls.acc_novel, cls.acc_seen, cls.H, cls.average_loss)) history.append([tf.convert_to_tensor(cls.acc_seen, dtype=tf.float32), tf.convert_to_tensor(cls.acc_novel, dtype=tf.float32), tf.convert_to_tensor(cls.H, dtype=tf.float32)]) else: print('[%.1f] acc=%.4f ' % (k, cls.acc)) history.append([0, tf.convert_to_tensor(cls.acc, dtype=tf.float32), 0]) if self.generalized: return tf.convert_to_tensor(cls.acc_seen, dtype=tf.float32), \ tf.convert_to_tensor(cls.acc_novel, dtype=tf.float32), \ tf.convert_to_tensor(cls.H, dtype=tf.float32) else: return 0, tf.convert_to_tensor(cls.acc, dtype=tf.float32), 0, history
def test_load_csv_data(): invalid_csv_file_path = 'invalid_csv_file_path' channels = 1 invalid_image_dimensions = (50, 77) invalid_target_labels = [8, 9, 10] # should raise error when not given csv column indices for images and labels with pytest.raises(ValueError): DataLoader(from_csv=True, target_labels=valid_target_labels, datapath=valid_csv_file_path, image_dimensions=valid_image_dimensions, csv_label_col=csv_label_col) # should raise error when given invalid csv file path with pytest.raises(FileNotFoundError): DataLoader(from_csv=True, target_labels=valid_target_labels, datapath=invalid_csv_file_path, image_dimensions=valid_image_dimensions, csv_label_col=csv_label_col, csv_image_col=csv_image_col) # should raise error when given invalid csv column indices with pytest.raises(ValueError): DataLoader(from_csv=True, target_labels=valid_target_labels, datapath=valid_csv_file_path, image_dimensions=valid_image_dimensions, csv_label_col=csv_label_col, csv_image_col=10) # should raise error when given empty target_labels list with pytest.raises(ValueError): DataLoader(from_csv=True, datapath=valid_csv_file_path, image_dimensions=valid_image_dimensions, csv_label_col=csv_label_col, csv_image_col=csv_image_col) # should raise error when not given image dimensions with pytest.raises(ValueError): DataLoader(from_csv=True, target_labels=valid_target_labels, datapath=valid_csv_file_path, csv_label_col=csv_label_col, csv_image_col=csv_image_col) # should raise error when given invalid image dimensions with pytest.raises(ValueError): DataLoader(from_csv=True, target_labels=valid_target_labels, datapath=valid_csv_file_path, image_dimensions=invalid_image_dimensions, csv_label_col=csv_label_col, csv_image_col=csv_image_col) # should raise error if no image samples found in csv file with pytest.raises(AssertionError): data_loader = DataLoader(from_csv=True, target_labels=invalid_target_labels, datapath=valid_csv_file_path, image_dimensions=valid_image_dimensions, csv_label_col=csv_label_col, csv_image_col=csv_image_col) data_loader.load_data() data_loader = DataLoader(from_csv=True, target_labels=valid_target_labels, datapath=valid_csv_file_path, image_dimensions=valid_image_dimensions, csv_label_col=csv_label_col, csv_image_col=csv_image_col) images, labels = data_loader.load_data() # should return non-empty image and label arrays when given valid arguments assert len(images) > 0 and len(labels) > 0 # should return same number of labels and images when given valid arguments assert len(images) == len(labels) # should reshape the images to given valid image_dimensions assert list(images.shape[1:]) == list(valid_image_dimensions) + [channels]
from src.config import config from src.augmentation import DataAugmentation from src.data_loader import DataLoader import sys if __name__ == '__main__': if len(sys.argv) > 1 and sys.argv[1] == 'augment': augmentation_flag = True else: augmentation_flag = False print "Folder to data : ", config.DATA_FOLDER loader = DataLoader(config.DATA_FOLDER) loader.prepare_images() if augmentation_flag: augment = DataAugmentation(loader) augment.perform_augmentation()
def __init__(self, hyperparameters): super(Model, self).__init__() self.auxiliary_data_source = hyperparameters['auxiliary_data_source'] self.all_data_sources = ['resnet_features', self.auxiliary_data_source] self.DATASET = hyperparameters['dataset'] self.num_shots = hyperparameters['num_shots'] self.latent_size = hyperparameters['latent_size'] self.batch_size = hyperparameters['batch_size'] self.hidden_size_rule = hyperparameters['hidden_size_rule'] self.warm_up = hyperparameters['model_specifics']['warm_up'] self.generalized = hyperparameters['generalized'] self.classifier_batch_size = 32 self.img_seen_samples = hyperparameters['samples_per_class'][self.DATASET][0] self.att_seen_samples = hyperparameters['samples_per_class'][self.DATASET][1] self.att_unseen_samples = hyperparameters['samples_per_class'][self.DATASET][2] self.img_unseen_samples = hyperparameters['samples_per_class'][self.DATASET][3] self.recog_loss_function = hyperparameters['loss'] self.num_epoch = hyperparameters['epochs'] self.lr_cls = hyperparameters['lr_cls'] self.cross_reconstruction = hyperparameters['model_specifics']['cross_reconstruction'] self.cls_train_epochs = hyperparameters['cls_train_steps'] self.dataset = DataLoader(self.DATASET, copy.deepcopy(self.auxiliary_data_source)) self.reparameterize_with_noise = False self.current_epoch = 0 if self.DATASET == 'CUB': self.num_classes = 200 self.num_novel_classes = 50 elif self.DATASET == 'SUN': self.num_classes = 717 self.num_novel_classes = 72 elif self.DATASET == 'AWA1' or self.DATASET == 'AWA2': self.num_classes = 50 self.num_novel_classes = 10 feature_dimensions = [2048, self.dataset.aux_data.size(1)] # Here, the encoders and decoders for all modalities are created and put into dict self.encoder = {} for datatype, dim in zip(self.all_data_sources,feature_dimensions): self.encoder[datatype] = models.encoder_template(dim, self.latent_size, self.hidden_size_rule[datatype]) print(str(datatype) + ' ' + str(dim)) self.decoder = {} for datatype, dim in zip(self.all_data_sources, feature_dimensions): self.decoder[datatype] = models.decoder_template(self.latent_size, dim, self.hidden_size_rule[datatype]) # An optimizer for all encoders and decoders is defined here self.parameters_to_optimize = list(self.parameters()) for datatype in self.all_data_sources: self.parameters_to_optimize += list(self.encoder[datatype].parameters()) self.parameters_to_optimize += list(self.decoder[datatype].parameters()) self.optimizer = tf.optimizers.Adam(learning_rate=hyperparameters['lr_gen_model'], beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0, amsgrad=True) if self.recog_loss_function == 'l2': # size_average=False self.reconstruction_criterion = tf.keras.losses.MSE() elif self.recog_loss_function == 'l1': # size_average=False self.reconstruction_criterion = tf.keras.losses.MAE()
if __name__ == '__main__': # region PARSE ARGS args = load_args() DIR_PATH = args.input_directory[0] DEPTH = args.depth RAW_DATA_FILENAME = args.raw_data_output DIR_PATH = Path(DIR_PATH).absolute() SAVE_DIR_PATH = Path(args.output_directory).absolute() try: FORMATS = list(map(str.strip, args.formats.split(','))) except AttributeError as e: FORMATS = None # endregion data_loader = DataLoader(DIR_PATH, formats=FORMATS) cnt_file = aux_function.cnt_files_in_folder( data_loader.get_stringify_path(), formats=FORMATS) for file_name, lines, depth in tqdm(data_loader.folder_walk(), total=cnt_file, desc='Progress'): relative_path = os.path.relpath(file_name, str(DIR_PATH)) processed_data = processing(lines) name, ext = os.path.splitext(relative_path) if not DEPTH or depth < DEPTH: saving_data_with_saving_tree(processed_lines=processed_data, relative_path_of_file=f'{name}.txt') else: append_data_to_file(processed_lines=processed_data, file_path=os.path.join(SAVE_DIR_PATH, RAW_DATA_FILENAME))
class Trainer(object): def __init__(self, **kwargs): self.parameters = kwargs self.logger = Logger(**kwargs) self.data_loader = DataLoader(**kwargs) self.model = GCN(input_dim=self.data_loader.get_input_feat_size(), hidden_dim=kwargs['hidden_dim'], num_classes=self.data_loader.get_num_classes(), dropout_prob=kwargs['dropout_prob'], bias=kwargs['bias']) self.optimizer = torch.optim.Adam(params=self.model.parameters(), lr=kwargs['lr']) self.cross_entropy = torch.nn.NLLLoss() def train(self): adj_matrix, feat_matrix, labels, _, val_indices, train_indices = self.data_loader.get_data( ) for epoch in range(self.parameters['num_epochs']): # training train_acc, train_loss = self.train_step(adj_matrix, feat_matrix, labels, train_indices) # validation val_loss, val_acc = self.inference_step(adj_matrix, feat_matrix, labels, val_indices) # logging self.logger.print_info(epoch + 1, train_loss.detach(), train_acc, val_loss, val_acc) self.logger.push_early_stopping(val_loss, self.model) if self.logger.early_stopping.early_stop: print("Early stopping") break # if the val_loss improves all epochs, we save the weights of the last model self.logger.early_stopping.save_checkpoint(val_loss, self.model) def train_step(self, adj_matrix, feat_matrix, labels, train_indices): self.model.train() self.optimizer.zero_grad() out = self.model(adj_matrix, feat_matrix) train_loss = self.loss(out[train_indices], labels[train_indices]) train_acc = utils.calculate_accuracy(out[train_indices], labels[train_indices]) train_loss.backward() self.optimizer.step() return train_acc, train_loss def test(self): # load the model saved at early stopping point state_dict_agent = torch.load(self.logger.save_folder + '/checkpoint.pt', map_location='cpu') self.model.load_state_dict(state_dict_agent) # inference test set adj_matrix, feat_matrix, labels, test_indices, val_indices, train_indices = self.data_loader.get_data( ) test_loss, test_acc = self.inference_step(adj_matrix, feat_matrix, labels, test_indices) print('\rTest Loss: {}, Test Acc: {}'.format(test_loss, test_acc)) def inference_step(self, adj_matrix, feat_matrix, labels, indices): """ Forward matrix and features and calculate loss and accuracy for the labels """ self.model.eval() out = self.model(adj_matrix, feat_matrix) loss = self.loss(out[indices], labels[indices]).detach() acc = utils.calculate_accuracy(out[indices], labels[indices]) return loss, acc def loss(self, predictions, labels): # calculate cross entropy loss with L2 regularization for the first layer parameters l2_reg = self.parameters['weight_decay'] * torch.sum( self.model.layer1.weights**2) loss = self.cross_entropy(predictions, labels) + l2_reg return loss
This script helps compute the different shapes of images in the dataset, and also the maximum dimensions across all the images in a set. ------------------------------------------------------------ Author : Anirudh Kumar Maurya Kakarlapudi, Aashish Yadavally """ import numpy as np from PIL import Image import os from collections import Counter from operator import itemgetter from src.data_loader import DataLoader import logging logger = logging.getLogger(__name__) dl = DataLoader() def different_sizes(key): """ Prints the counter of different image shape in the data Arguments: --------- key : string One of 'train', 'test' or 'masks' Returns: ------- dimension_counter: Counter Counter of each image shape
origins = [ "http://localhost.tiangolo.com", "https://localhost.tiangolo.com", "http:localhost", "http:localhost:8080", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) data_loader = DataLoader(os.path.join(PATH_DATA_DIRECTORY, '1')) yield_estimator = YieldEstimator() yield_estimator.fit(data_loader.df_previous_production) cu_estimator = CuEstimator() cu_estimator.fit(data_loader.df_previous_production) @app.get("/") def test_data(): return { 'x': ['giraffes', 'orangutans', 'monkeys'], 'y': [20, 14, 23], }
def test_load_time_series_directory_data(): invalid_directory_path = 'invalid_directory_path' valid_dummy_directory = './resources/dummy_time_series_data_directory' empty_dummy_directory = './resources/dummy_empty_data_directory' valid_time_steps = 4 channels = 1 # should raise error when receives an invalid directory path with pytest.raises(NotADirectoryError): DataLoader(from_csv=False, datapath=invalid_directory_path, time_steps=4) # should raise error when tries to load empty directory data_loader = DataLoader(from_csv=False, datapath=empty_dummy_directory, time_steps=4) with pytest.raises(AssertionError): data_loader.load_data() # should raise error when given time_step argument that is less than 1 with pytest.raises(ValueError): DataLoader(from_csv=False, datapath=valid_dummy_directory, time_steps=-4) # should raise error when given time_step argument that not an integer with pytest.raises(ValueError): DataLoader(from_csv=False, datapath=valid_dummy_directory, time_steps=4.7) # should raise error when tries to load time series sample # containing a quantity of images less than the time_steps argument with pytest.raises(ValueError): data_loader = DataLoader(from_csv=False, datapath=valid_dummy_directory, time_steps=10) data_loader.load_data() # should assign an image's parent directory name as its label data_loader = DataLoader(from_csv=False, datapath=valid_dummy_directory, time_steps=valid_time_steps) samples, labels, label_index_map = data_loader.load_data() label_count = len(label_index_map.keys()) label = [0] * label_count label[label_index_map['happiness']] = 1 assert label == labels[0] data_loader = DataLoader(from_csv=False, datapath=valid_dummy_directory, time_steps=valid_time_steps) samples, labels, label_index_map = data_loader.load_data() # should return non-empty image and label arrays when given valid arguments assert len(samples) > 0 and len(labels) > 0 # should return same number of labels and images when given valid arguments assert len(samples) == len(labels) # should reshape image to contain channel_axis in channel_last format assert samples.shape[1] == valid_time_steps # should reshape image to contain channel_axis in channel_last format assert samples.shape[-1] == channels
def load_data(data_path, window_size): data_loader = DataLoader(data_path, window_size) train_data, label_data = data_loader.load_train_data() return train_data, label_data
def main(train=True): niter = 50 niter_snapshot = 2048 batch_size = 16 data_size = 64 filters = 64 dim_z = 128 kt = 0.0 gamma = 0.4 multiplier = 1e-3 lr = 1e-4 ckpt_dir = "./" max_to_keep = 5 # load the data data = DataLoader(path="./data/img_align_celeba_png") batch_total = int(data.size() / batch_size) with tf.name_scope("model"): model = BEGAN(batch_size, data_size, filters, dim_z, dim_z, gamma) with tf.name_scope("train"): opt_g, g_loss, d_real_loss, d_fake_loss = model.generator_ops() opt_d, d_loss = model.discriminator_ops() g_opt = [opt_g, g_loss, d_real_loss, d_fake_loss] d_opt = [opt_d, d_loss] with tf.name_scope("miscellaneous"): init = tf.global_variables_initializer() saver = tf.train.Saver(max_to_keep=(max_to_keep)) # if we're now training the model if train: with tf.Session() as sess: count = 0 k_list = [] loss_list = [] # initializer sess.run(init) try: ckpt = tf.train.get_checkpoint_state(ckpt_dir) ckpt_name = os.path.basename(ckpt.model_checkpoint_path) saver.restore(sess, os.path.join(ckpt_dir, ckpt_name)) except: saver.save(sess, "./model.ckpt", write_meta_graph=True) for epoch in range(niter): for idx in range(0, batch_total): count += 1 batch_x = np.random.uniform(-1., 1., size=[batch_size, dim_z]) batch_data = data.batch(batch_size=batch_size) # opt & feed list (different with paper) feed_dict = { model.x: batch_x, model.y: batch_data, model.kt: kt, model.lr: lr } # run tensorflow _, loss_g, d_real_loss, d_fake_loss = sess.run( g_opt, feed_dict=feed_dict) _, loss_d = sess.run(d_opt, feed_dict=feed_dict) # update kt, m_global kt = kt + multiplier * (gamma * d_real_loss - d_fake_loss) kt = np.clip(kt, 0.0, 1.0) m_global = d_real_loss + np.abs(gamma * d_real_loss - d_fake_loss) loss = loss_g + loss_d k_list.append(kt) loss_list.append(m_global) print( "Epoch: [%2d] [%4d/%4d], loss: %.4f, loss_g: %.4f, loss_d: %.4f, d_real: %.4f, d_fake: %.4f, kt: %.8f, M: %.8f" % (epoch, idx, batch_total, loss, loss_g, loss_d, d_real_loss, d_fake_loss, kt, m_global)) # Test during Training if count % niter_snapshot == (niter_snapshot - 1): # update learning rate lr *= 0.95 # save & test saver.save(sess, "./model.ckpt", global_step=count, write_meta_graph=False) test_data = np.random.uniform(-1., 1., size=[batch_size, dim_z]) output_gen = sess.run(model.recon_gen, feed_dict={model.x: test_data}) output_dec = sess.run(model.recon_dec, feed_dict={model.x: test_data}) fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot(2, 2, 3) ax4 = fig.add_subplot(2, 2, 4) ax1.plot(np.array(k_list), 'k--', label="k") ax2.plot(np.array(loss_list), 'k-', label="m_global") ax3.imshow(output_dec[0]) ax4.imshow(batch_data[0]) fig.show()