def recover(actual_z): mse_loss = nn.MSELoss() #mse_loss_ = nn.MSELoss() #z_approx = torch.FloatTensor(1, nz, 1, 1).uniform_(-1, 1) device = torch.device("cuda:0" if ( torch.cuda.is_available() and ngpu > 0) else "cpu") torch.manual_seed = random.randint(1, 10000) z_approx = torch.randn(1, nz, 1, 1, device=device) z_approx = Variable(z_approx) z_approx.requires_grad = True optimizer = optim.Adam([z_approx], lr=learning_rate, betas=(.5, .999)) show_image(gan.generate(z_approx)[0]) for i in range(num_updates): g_z_approx = gan.generate(z_approx)[0] assert g_z_approx.shape == actual_z.shape mse_g_z = mse_loss(g_z_approx, actual_z) #mse_z = mse_loss_(z_approx, z) if i % 15 == 0: print("[Iter {}] MSE: {}".format(i, mse_g_z.item())) show_image(g_z_approx) # bprop optimizer.zero_grad() mse_g_z.backward() optimizer.step() #best_image = gan.generate(g_z_approx)[0] show_image(gan.generate(z_approx)[0]) return z_approx
def gen(): tb._SYMBOLIC_SCOPE.value = False values = [x for x in request.form.values()] if len(values) > 1: logging.debug( "ERROR: Invalid number of genres, expected only one genre or none for all genres" ) return jsonify("Error: Invalid number of inputs") elif len(values) == 1 and values[0] != '': genre = values[0] else: genre = 'any' logging.info("Begin generating track") genre = values[0] #Load the generator's model from its stored location model_name = ROOT_DIR + '/models/model_' + genre + '.h5' model = load_model(model_name) #Loads the notes that were recieved when the model was originally trained logging.info('Retrieve notes from %s', genre) notes = pickle.load(open(ROOT_DIR + '/data/notes/' + genre + '.txt', 'rb')) #Generate the notes for the new drumtrack #Create a new MIDI file and save it to be played logging.info('Generate notes for the new track') predictions = g.generate(model, notes) midi_result = g.create_midi(predictions) midi_name = ROOT_DIR + "/static/result_midi.mid" midi_result.write(midi_name) logging.info('New track is created, (%s)', midi_name) return jsonify(midi_name)
def generate(): logging.info('Begin generating track..') tb._SYMBOLIC_SCOPE.value = False values = [x for x in request.form.values()] genre = int(values[0]) #Load the generator's model from its stored location model_name = ROOT_DIR + '/models/model_' + genres[genre] + '.h5' model = load_model(model_name) #Loads the notes that were recieved when the model was originally trained logging.info('Retrieve notes from %s', genre) notes = pickle.load( open(ROOT_DIR + '/data/notes/' + genres[genre] + '.txt', 'rb')) #Generate the notes for the new drumtrack #Create a new MIDI file and save it to be played logging.info('Generate notes for the new track') predictions = g.generate(model, notes) midi_result = g.create_midi(predictions) filename = str(uuid.uuid4()) + ".mid" midi_name = ROOT_DIR + "/static/" + filename #midi_name = ROOT_DIR+"/static/result_midi.mid" midi_result.write(midi_name) logging.info('New track is created, (%s)', midi_name) return render_template('index.html', prediction_text='Drumtrack Generated!', value=filename)
def __init__(self, root, train = True, transform = None, target_transform = None, download = False , if_gan = True ): self.root = root dir_path = self.root dataset_training_file = dir_path + '/trainingData.csv' dataset_validation_file = dir_path + '/validationData.csv' # Load independent variables (WAPs values) if train: dataset_file = dataset_training_file else: dataset_file = dataset_validation_file file = open(dataset_file, 'r') # Load labels label = file.readline() label = label.split(',') # Load independent variables file_load = np.loadtxt(file, delimiter = ',', skiprows = 0) # RSSI values self.x = file_load[:, 0 : 520] # Load dependent variables self.y = file_load[:, 520 : 524] # Divide labels into x and y self.x_label = label[0 : 520] self.x_label = np.concatenate([self.x_label, label[524: 529]]) self.y_label = label[520 : 524] # Regularization of independent variables self.x[self.x == 100] = -104 # WAP not detected self.x = self.x + 104 # Convert into positive values self.x = self.x / 104 # Regularize into scale between 0 and 1 # Building ID, Space ID, Relative Position, User ID, Phone ID and Timestamp respectively file.close() # Reduce the number of dependent variables by combining building number and floor into one variable: area # 5*buliding + floor area = self.y[:, 3] * 4 + self.y[:, 2] #print(self.y[0].size) #print(area[0].size) #self.y = np.column_stack((self.y, area)) self.y = area if if_gan==1 : number_generator = 10000 generator_data,fixed_label = gan.generate(number_generator) generator_data = generator_data.cpu() generator_data = generator_data.detach().numpy() fixed_label = fixed_label.cpu() fixed_label = fixed_label.detach().numpy() #---------------------concatenate gan and label----------------------# fixed_label = fixed_label.flatten() self.y = np.concatenate([self.y,fixed_label]) self.x = np.vstack([self.x,generator_data])
def main(img_path, optimizer_choice, name): print("running program") ##initialize gan model to generate stuff #define loss function loss_fn = torch.nn.MSELoss(reduction='mean') #load in actual image from file path into tensor actual_image = load_image(img_path) show_image(actual_image) #FOR TESTING # # # # print("shape of actual image: ") # # print(actual_image.size()) # # show_image(actual_image) # # device1 = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu") # # z = torch.randn(1, nz, 1, 1, device=device1, requires_grad=True) # z_hat = z.clone() # optimizer = torch.optim.Adam([z], .0005) # # #print(torch.eq(z, y).all().item()) # 1 means same, 0 means different # gen_image = gan.generate(z)[0] # # print("shape of generated image: ") # # print(gen_image.size()) # # show_image(gen_image) # # loss = loss_fn(gen_image, actual_image) # loss = Variable(loss, requires_grad=True) # # print("LOSS INFO") # print(loss.item()) # optimizer.zero_grad() # loss.backward() # optimizer.step() # # if torch.eq(z, z_hat).all().item() == 1: # print("Tensors are same") # else: # print("tensors are diff") #TESTING #outer loop -> defines the number of restarts num_restarts = 10000 num_updates = 2 min_loss = 1e10 best_z = 0 for i in range(num_restarts): #generate vector to pass to model device = torch.device("cuda:0" if ( torch.cuda.is_available() and ngpu > 0) else "cpu") z = torch.randn(1, nz, 1, 1, device=device, requires_grad=True) learning_rate = .1 optimizer = getOptimizer(optimizer_choice, [z], learning_rate) gen_image = gan.generate(z)[0] loss = loss_fn(gen_image, actual_image) if (loss.item() < min_loss): min_loss = loss.item() best_z = z #inner loop to run gradient descent on the z vector # for j in range(num_updates): # gen_image = gan.generate(z) # show_image(gen_image) # loss = loss_fn(gen_image, actual_image) # print("Loss for run " + str(i) + " round " + str(j) + " : " + str(loss.item())) # # if loss.item() < min_loss: # min_loss = loss.item() # best_z = z # loss = Variable(loss, requires_grad=True) # # # optimizer.zero_grad() # z.retain_grad() # loss.backward() # print(z.grad) # optimizer.step() best_image = gan.generate(best_z)[0] show_image(best_image)
def main(img_path, optimizer_choice, name): print("running program") ##initialize gan model to generate stuff #define loss function loss_fn = torch.nn.MSELoss(reduction='mean') #load in actual image from file path into tensor actual_image = load_image(img_path) show_image(actual_image) #FOR TESTING # # # # print("shape of actual image: ") # # print(actual_image.size()) # # show_image(actual_image) # # device1 = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu") # # z = torch.randn(1, nz, 1, 1, device=device1, requires_grad=True) # z_hat = z.clone() # optimizer = torch.optim.Adam([z], .0005) # # #print(torch.eq(z, y).all().item()) # 1 means same, 0 means different # gen_image = gan.generate(z)[0] # # print("shape of generated image: ") # # print(gen_image.size()) # # show_image(gen_image) # # loss = loss_fn(gen_image, actual_image) # loss = Variable(loss, requires_grad=True) # # print("LOSS INFO") # print(loss.item()) # optimizer.zero_grad() # loss.backward() # optimizer.step() # # if torch.eq(z, z_hat).all().item() == 1: # print("Tensors are same") # else: # print("tensors are diff") #TESTING #outer loop -> defines the number of restarts num_restarts = 3 num_updates = 5000 learning_rate = .005 global_min_loss = 1e10 best_z = [] for i in range(num_restarts): #generate vector to pass to model device = torch.device("cuda:0" if ( torch.cuda.is_available() and ngpu > 0) else "cpu") #gnerate vector here z = torch.randn(1, nz, 1, 1, device=device, requires_grad=False) local_min_loss = 1e10 for j in range(num_updates): update_index = random.randint(0, 99) to_mod = learning_rate * random.randint(1, 5) * 10 np_arr1 = z.numpy() np_arr1[:, update_index, :, :] = np_arr1[:, update_index, :, :] + to_mod new_tensor_add = torch.tensor(np_arr1, device=device) add_gen = gan.generate(new_tensor_add)[0] add_loss = loss_fn(add_gen, actual_image) np_arr2 = z.numpy() np_arr2[:, update_index, :, :] = np_arr2[:, update_index, :, :] - to_mod new_tensor_sub = torch.tensor(np_arr2, device=device) sub_gen = gan.generate(new_tensor_sub)[0] sub_loss = loss_fn(sub_gen, actual_image) if add_loss.item() < local_min_loss: z = new_tensor_add local_min_loss = add_loss.item() elif sub_loss.item() < local_min_loss: z = new_tensor_sub local_min_loss = sub_loss.item() print("Restart #" + str(i) + " run #" + str(j) + " local_loss: " + str(local_min_loss)) if local_min_loss < global_min_loss: best_z = z global_min_loss = local_min_loss print("End of round " + str(i) + ": global loss is " + str(global_min_loss)) best_image = gan.generate(best_z)[0] show_image(best_image)