def ex_info_gan(): data_sets = ReWrite.load_data_in_seq(source_files) data_sets = ReWrite.MyDataSet(data_sets) data_loader = DataLoader( data_sets, batch_size=256, shuffle=True, ) generator = G_D_Module.GeneratorInfo(latent_dim=50, n_classes=5, code_dim=2, img_shape=img_shape) discriminator = G_D_Module.DiscriminatorInfo(n_classes=5, code_dim=2, img_shape=img_shape) TrainFunction.train_info_gan(generator, discriminator, data_loader, opt.n_epochs, opt.lr, opt.b1, opt.b2, latent_dim=50, n_classes=5, code_dim=2, cuda=cuda, first_train=False)
def ex_ponodcwcgan(): data_sets = ReWrite.load_data_in_seq(source_files) data_sets = ReWrite.MyDataSet(data_sets) data_loader = DataLoader( data_sets, batch_size=256, shuffle=True, ) latent_dim = 100 generator = G_D_Module.GeneratorPONODCWCGAN( latent_dim, opt.n_classes, img_shape) # latent_dim should be 20 discriminator = G_D_Module.DiscriminatorPONODCWCGAN( opt.n_classes, img_shape) TrainFunction.train_ponodcwcgan(generator, discriminator, data_loader, opt.n_epochs, opt.lr, opt.b1, opt.b2, latent_dim, opt.n_classes, cuda, fist_train=False)
def show_1d_data(): real_data = read_csv_data() n_class = len(real_data) weight = 2 index = np.random.randint(450, size=(5, 2)) for i in range(n_class): for j in range(weight): plt.subplot(n_class, weight, i * weight + j + 1) plt.xticks([]) # index = random.randint(0, real_data[i].shape[0] - 1) plt.plot(real_data[i][index[i][j]]) plt.savefig('caches/real_1d_data.jpg', bbox_inches='tight') plt.close() cuda = torch.cuda.is_available() FloatTensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor LongTensor = torch.cuda.LongTensor if cuda else torch.LongTensor latent_dim = 50 generator = G_D_Module.GeneratorConv1D(latent_dim, n_class) generator.load_state_dict( torch.load('GANParameters/CONV1DGAN/generator.pt')) if cuda: generator.cuda() noise = FloatTensor( np.random.normal(0, 1, (n_class * weight, 1, latent_dim))) labels = LongTensor(list(range(0, n_class)) * weight) gen_data = generator(noise, labels) gen_data = gen_data.cpu().detach().numpy() for i in range(gen_data.shape[0]): plt.subplot(n_class, weight, i + 1) plt.xticks([]) # plt.xlabel(str(i // weight)) plt.plot(gen_data[i % n_class][0]) plt.savefig('caches/gen_conv_1d_data.jpg', bbox_inches='tight') plt.close() generator = G_D_Module.GeneratorSelfNoise1D() if cuda: generator.cuda() inputs = np.empty((n_class * weight, 1, 1024), dtype=float) for i in range(inputs.shape[0]): # index = random.randint(0, real_data[i // weight].shape[0] - 1) inputs[i][0] = real_data[i // weight][index[i // 2][i % 2]] inputs = FloatTensor(inputs) gen_data = generator(inputs) gen_data = gen_data.cpu().detach().numpy() for i in range(gen_data.shape[0]): plt.subplot(n_class, weight, i + 1) plt.xticks([]) # plt.xlabel(str(i // weight)) plt.plot(gen_data[i][0]) plt.savefig('caches/gen_selfnoise_1d_data.jpg', bbox_inches='tight') plt.close()
def gan_data_selfnoise_1d_perf(data_epoch_num, cuda=True): n_class = 5 if cuda: FloatTensor = torch.cuda.FloatTensor LongTensor = torch.cuda.LongTensor else: FloatTensor = torch.FloatTensor LongTensor = torch.LongTensor generator = G_D_Module.GeneratorSelfNoise1D() generator.load_state_dict( torch.load('GANParameters/SELFNOISE1DGAN/generator.pt')) if cuda: generator.cuda() datas = np.empty((n_class, data_epoch_num, 1024), dtype=float) data_list = read_csv_data() for i in range(data_epoch_num): data = np.empty((n_class, 1, 1024), dtype=float) for j in range(n_class): index = random.randint(0, data_list[j].shape[0] - 1) data[j][0] = data_list[j][index] data = FloatTensor(data) gen_data = generator(data).cpu().detach().numpy() for j in range(n_class): datas[j][i] = gen_data[j][0] print('[%d/%d]' % (i, data_epoch_num)) np.savez(save_path + "gen_1d/data_selfnoise.npz", datas)
def dcwcgan_data(img_epoch_num=50, cuda=True): ''' :param img_epoch_num: :param cuda: :return: imgs(.jpg) note: change the save path ''' latent_dim = 20 # details in G_D_Module n_class = 5 # details in G_D_Module img_shape = (1, 32, 32) # details in G_D_Module data_list = os.listdir('coedatas') data = [] for path in data_list: data.append(data_read('coedatas/' + path)) if cuda: FloatTensor = torch.cuda.FloatTensor LongTensor = torch.cuda.LongTensor generator = G_D_Module.GeneratorDCWCGAN(latent_dim, n_class, img_shape) generator.load_state_dict(torch.load('GANParameters/DCWCGAN/generator.pt')) noise = FloatTensor( np.random.normal(0, 1, (img_epoch_num * n_class, latent_dim))) single_list = list(range(n_class)) label_cpu = single_list * img_epoch_num label = LongTensor(label_cpu) if cuda: label.cuda() generator.cuda() gen_imags = generator(noise, label) gen_imags = gen_imags.cpu() for i in range(gen_imags.shape[0]): plt.axis('off') plt.contourf(gen_imags[i][0].detach().numpy()) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig(save_path + 'gen/label' + str(i % 5) + '_' + str(i // 5) + '.jpg') plt.close() for i in range(img_epoch_num): for j in range(len(data)): index = random.randint(0, data[j].shape[0] - 1) plt.axis('off') plt.contourf(data[j][index]) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig(save_path + 'real/label' + str(j) + '_' + str(i) + '.jpg') plt.close()
def self_noise_data(img_epoch_num=50, cuda=True): ''' :param img_epoch_num: :param cuda: :return: imgs(.jpg) note: change the save path ''' img_shape = (1, 32, 32) # details in G_D_Module data_list = os.listdir('coedatas') data = [] for path in data_list: data.append(data_read('coedatas/' + path)) FloatTensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor generator = G_D_Module.GeneratorSelfNoise(img_shape) generator.load_state_dict( torch.load('GANParameters/SELFNOISEGAN/generator.pt')) if cuda: generator.cuda() for epoch in range(img_epoch_num): imgs = np.empty([len(data), 1, 32, 32], dtype=float) for i in range(len(data)): index = random.randint(0, len(data[i]) - 20 - 1) imgs[i][0] = data[i][index] if cuda: imgs_torch = FloatTensor(imgs).cuda() gen_imags = generator(imgs_torch) gen_imags = gen_imags.cpu() for i in range(gen_imags.shape[0]): plt.axis('off') plt.contourf(gen_imags[i][0].detach().numpy()) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig(save_path + 'gen/label' + str(i) + '_' + str(epoch) + '.jpg') plt.close() for i in range(imgs.shape[0]): plt.axis('off') plt.contourf(imgs[i][0]) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig(save_path + 'real/label' + str(i) + '_' + str(epoch) + '.jpg') plt.close()
def ex_self_noise_gan(): data_sets = ReWrite.load_data_in_seq(source_files) data_sets = ReWrite.MyDataSet(data_sets) data_loader = DataLoader( data_sets, batch_size=256, shuffle=True, ) generator = G_D_Module.GeneratorSelfNoise(img_shape) discriminator = G_D_Module.DiscriminatorSelfNoise(img_shape) TrainFunction.train_self_noise_gan(generator, discriminator, data_loader, opt.n_epochs, opt.lr, opt.b1, opt.b2, cuda, first_train=False)
def self_noise_data_perf(img_epoch_num=50, cuda=True): """ :param img_epoch_num: :param cuda: :return: .npz in directory of gen """ n_class = 5 # details in G_D_Module img_shape = (1, 32, 32) # details in G_D_Module data_list = os.listdir('coedatas') data = [] for path in data_list: data.append(data_read('coedatas/' + path)) if cuda: FloatTensor = torch.cuda.FloatTensor LongTensor = torch.cuda.LongTensor else: FloatTensor = torch.FloatTensor LongTensor = torch.LongTensor generator = G_D_Module.GeneratorSelfNoise(img_shape) generator.load_state_dict( torch.load('GANParameters/SELFNOISEGAN/generator.pt')) end_list = None for i in range(img_epoch_num // 50): datas_list = [] for j in range(n_class): mid = np.empty([50, 1, 32, 32], dtype=float) for k in range(50): mid[k][0] = data[j][random.randint(0, data[j].shape[0] - 1)] datas_list.append(mid) img_list = [] for img in datas_list: if cuda: generator.cuda() img = FloatTensor(img) gen_imags = generator(img) gen_imags = gen_imags.cpu() img_list.append(gen_imags) if end_list is None: end_list = [ img_list[mid].detach().numpy() for mid in range(len(img_list)) ] else: for count in range(len(end_list)): end_list[count] = np.concatenate( (end_list[count], img_list[count].detach().numpy()), axis=0) for i in range(len(end_list)): np.savez(save_path + "gen/%d.npz" % i, end_list[i]) print(end_list[i].shape)
def ex_selfnoise_1d_gan(): data_sets = ReWrite.load_data_in_seq_1d('data') data_sets = ReWrite.MyDataSet1D(data_sets) data_loader = DataLoader( data_sets, batch_size=128, shuffle=True, ) generator = G_D_Module.GeneratorSelfNoise1D() discriminator = G_D_Module.DiscriminatorSelfNoise1D() TrainFunction.train_selfnoise_1d_gan(generator, discriminator, data_loader, opt.n_epochs, opt.lr, opt.b1, opt.b2, -1, opt.n_classes, cuda, first_train=False)
def ex_linear1d_gan(): data_sets = ReWrite.load_data_in_seq_1d('data') data_sets = ReWrite.MyDataSet1D(data_sets) data_loader = DataLoader( data_sets, batch_size=512, shuffle=True, ) latent_dim = 50 generator = G_D_Module.GeneratorLinear1D(latent_dim, opt.n_classes) discriminator = G_D_Module.DiscriminatorLinear1D(opt.n_classes) TrainFunction.train_linear_1d_gan(generator, discriminator, data_loader, opt.n_epochs, opt.lr, opt.b1, opt.b2, latent_dim, opt.n_classes, cuda, first_train=False)
def show_self_noise_data(): data_list = os.listdir('coedatas') data = [] for path in data_list: data.append(data_read('coedatas/' + path)) FloatTensor = torch.cuda.FloatTensor generator = G_D_Module.GeneratorSelfNoise((1, 32, 32)) generator.load_state_dict( torch.load('GANParameters/SELFNOISEGAN/generator.pt')) generator.cuda() imgs = np.empty([len(data)**2, 1, 32, 32], dtype=float) for i in range(len(data)): for j in range(len(data)): index = random.randint(0, len(data[j]) - 1) imgs[i * len(data) + j][0] = data[j][index] imgs_torch = FloatTensor(imgs) gen_imags = generator(imgs_torch) for i in range(gen_imags.size(0)): plt.subplot(len(data), len(data), i + 1) plt.axis('off') plt.contourf(gen_imags[i][0].detach().cpu().numpy()) plt.savefig('caches/gen.jpg', bbox_inches='tight', pad_inches=0) # plt.show() plt.close() # random imgs = np.empty([len(data)**2, 1, 32, 32], dtype=float) for i in range(len(data)): for j in range(len(data)): index = random.randint(0, len(data[j]) - 1) imgs[i * len(data) + j][0] = data[j][index] # r for i in range(len(imgs)): plt.subplot(len(data), len(data), i + 1) plt.axis('off') plt.contourf(imgs[i][0]) plt.savefig('caches/real.jpg', bbox_inches='tight', pad_inches=0.0) # plt.show() plt.close()
def dcwcgan_data_perf(img_epoch_num=50, cuda=True): ''' :param img_epoch_num: :param cuda: :return: note: generate the .npy in the Performance ''' latent_dim = 20 # details in G_D_Module n_class = 5 # details in G_D_Module img_shape = (1, 32, 32) # details in G_D_Module data_list = os.listdir('coedatas') data = [] for path in data_list: data.append(data_read('coedatas/' + path)) if cuda: FloatTensor = torch.cuda.FloatTensor LongTensor = torch.cuda.LongTensor else: FloatTensor = torch.FloatTensor LongTensor = torch.LongTensor generator = G_D_Module.GeneratorDCWCGAN(latent_dim, n_class, img_shape) generator.load_state_dict(torch.load('GANParameters/DCWCGAN/generator.pt')) noise = FloatTensor( np.random.normal(0, 1, (img_epoch_num * n_class, latent_dim))) single_list = list(range(n_class)) label_cpu = single_list * img_epoch_num label = LongTensor(label_cpu) if cuda: label.cuda() generator.cuda() gen_imags = generator(noise, label) gen_imags = gen_imags.cpu() datas = [] for i in range(len(data_list)): datas.append(np.empty((img_epoch_num, *img_shape), dtype=float)) for i in range(len(label_cpu)): datas[label_cpu[i]][i // 5][0] = gen_imags[i][0].detach().numpy() for i in range(len(datas)): np.savez(save_path + "gen/%d.npz" % i, datas[i]) print(datas[i].shape)
def show_dcwcgan_data(): im_index = range(9) latent_dim = 20 data_list = os.listdir('coedatas') data = [] for path in data_list: data.append(data_read('coedatas/' + path)) FloatTensor = torch.FloatTensor LongTensor = torch.LongTensor generator = G_D_Module.GeneratorDCWCGAN(latent_dim, 5, (1, 32, 32)) generator.load_state_dict(torch.load('GANParameters/DCWCGAN/generator.pt')) noise = FloatTensor(np.random.normal(0, 1, (len(data)**2, latent_dim))) single_list = list(range(len(data))) label = LongTensor(single_list * len(data)) gen_imags = generator(noise, label) gen_imags = gen_imags.cpu() for i in range(gen_imags.size(0)): plt.subplot(len(data), len(data), i + 1) plt.axis('off') plt.contourf(gen_imags[i][0].detach().numpy()) # plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig('caches/gen.jpg', bbox_inches='tight', pad_inches=0) # plt.show() plt.close() for i in range(len(data)): for j in range(len(data)): index = random.randint(0, data[j].shape[0] - 1) plt.subplot(len(data), len(data), i * len(data) + j + 1) plt.axis('off') plt.contourf(data[j][index]) # plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig('caches/real.jpg', bbox_inches='tight', pad_inches=0) # plt.show() plt.close()
def gan_data_1d_perf(img_epoch_num, cuda=True): """This is WCGAN1D to performance :param img_epoch_num: :param cuda: :return: gen_1d/data_wcgan.npz """ latent_dim = 50 # details in G_D_Module n_class = 5 # details in G_D_Module if cuda: FloatTensor = torch.cuda.FloatTensor LongTensor = torch.cuda.LongTensor else: FloatTensor = torch.FloatTensor LongTensor = torch.LongTensor generator = G_D_Module.GeneratorConv1D(latent_dim, n_class) generator.load_state_dict( torch.load('GANParameters/CONV1DGAN/generator.pt')) noise = np.random.normal(0, 1, (img_epoch_num * n_class, latent_dim)) noise = noise[:, np.newaxis, :] noise = FloatTensor(noise) single_list = list(range(n_class)) label_cpu = single_list * img_epoch_num label = LongTensor(label_cpu) if cuda: label.cuda() generator.cuda() gen_datas = generator(noise, label) gen_datas = gen_datas.cpu() datas = np.empty((n_class, img_epoch_num, 1024), dtype=float) for i in range(len(label_cpu)): datas[label_cpu[i]][i // 5] = gen_datas[i].detach().numpy() np.savez(save_path + "gen_1d/data_wcgan.npz", datas)
def show_cdcgan_data(): latent_dim = 20 data_list = os.listdir('coedatas') data = [] for path in data_list: data.append(data_read('coedatas/' + path)) FloatTensor = torch.FloatTensor LongTensor = torch.LongTensor generator = G_D_Module.GeneratorCDCGAN(latent_dim, 5, (1, 32, 32)) generator.load_state_dict(torch.load('GANParameters/CDCGAN/generator.pt')) noise = FloatTensor(np.random.normal(0, 1, (len(data)**2, latent_dim))) single_list = list(range(len(data))) label = LongTensor(single_list * len(data)) gen_imags = generator(noise, label) # real # imgs = np.empty([len(data) ** 2, 1, 32, 32], dtype=float) # for i in range(len(data)): # for j in range(len(data)): # index = random.randint(0, len(data[j]) - 1) # imgs[i * len(data) + j][0] = data[j][index] # for i in range(imgs.shape[0]): # plt.subplot(len(data_list), len(data_list), i + 1) # plt.axis('off') # plt.contourf(imgs[i][0]) # plt.savefig('caches/real.jpg', bbox_inches='tight') # plt.close() for i in range(gen_imags.shape[0]): plt.subplot(len(data), len(data), i + 1) plt.axis('off') plt.contourf(gen_imags[i][0].detach().numpy()) plt.savefig('caches/gen.jpg', bbox_inches='tight') plt.close()
def img_data(img_epoch_num, cuda=True): ''' :param img_epoch_num: :param cuda: :return: imgs(.jpg) note: change the save path ''' os.makedirs('Performance/imgdatas/train/gen', exist_ok=True) os.makedirs('Performance/imgdatas/train/real', exist_ok=True) os.makedirs('Performance/imgdatas/test', exist_ok=True) img_shape = (1, 32, 32) # details in G_D_Module data_list = os.listdir('coedatas') data = [] for path in data_list: data.append(data_read('coedatas/' + path)) FloatTensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor generator = G_D_Module.GeneratorSelfNoise(img_shape) generator.load_state_dict( torch.load('GANParameters/SELFNOISEGAN/generator.pt')) if cuda: generator.cuda() for epoch in range(img_epoch_num): imgs = np.empty([len(data), 1, 32, 32], dtype=float) for i in range(len(data)): index = random.randint(0, len(data[i]) - 20 - 1) imgs[i][0] = data[i][index] imgs_torch = FloatTensor(imgs) gen_imags = generator(imgs_torch) gen_imags = gen_imags.cpu() for i in range(gen_imags.shape[0]): plt.axis('off') plt.contourf(gen_imags[i][0].detach().numpy()) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig('Performance/imgdatas/train/gen/' + str(i) + '_' + str(epoch) + '.jpg', pad_inches=0.0) plt.close() # real data 300 for train 170+ for test don't commend if need for i in range(len(data)): for j in range(data[i].shape[0]): if j < 300: plt.axis('off') plt.contourf(data[i][j]) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig('Performance/imgdatas/train/real/' + str(i) + '_' + str(j) + '.jpg', pad_inches=0.0) plt.close() else: plt.axis('off') plt.contourf(data[i][j]) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.savefig('Performance/imgdatas/test/' + str(i) + '_' + str(j) + '.jpg', pad_inches=0.0) plt.close()