Beispiel #1
0
        sampling(bigimage,
                 biglabel,
                 numm + 1,
                 opt.dataroot + '/data',
                 rand_num=56)
    if opt.resume == 'Y':
        # Load state dicts
        netG_A2B.load_state_dict(torch.load(opt.generator_A2B))
        netG_B2A.load_state_dict(torch.load(opt.generator_B2A))
        netD_A.load_state_dict(torch.load(opt.discriminator_A))
        netD_B.load_state_dict(torch.load(opt.discriminator_B))

        # Set model's test mode
        netG_A2B.eval()
        netG_B2A.eval()
        netD_A.eval()
        netD_B.eval()

    else:
        netG_A2B.apply(weights_init_normal)
        netG_B2A.apply(weights_init_normal)
        netD_A.apply(weights_init_normal)
        netD_B.apply(weights_init_normal)

    # Lossess
    criterion_GAN = torch.nn.MSELoss()
    criterion_cycle = torch.nn.L1Loss()
    criterion_identity = torch.nn.L1Loss()
    criterion_BCE = torch.nn.BCEWithLogitsLoss()

    # Optimizers & LR schedulers

print('Saving Features')
if not os.path.exists(feature_file):
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size,
                    shuffle=True, num_workers=opt.num_workers)

    
    netD = Discriminator(opt.ndf, opt.nc, opt.filters, opt.strides, opt.padding)
    netD.cuda()
    
    epoch = 10
    netD.load_state_dict(torch.load(opt.model_path + 'netD_epoch_{}.pth'.format(epoch)))
    
    print(netD)
    netD.eval()
    n_features = 4096 # 1024x2x2
    save_features(dataloader, opt.batch_size, n_features, feature_file)

print('Load Features')
data = np.loadtxt(feature_file, dtype=np.float16)

features, labels = data[:, : -1], data[:, -1: ]
shape = features.shape

print('Data has {} samples and {} features '.format(shape[0], shape[1]))
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.33, random_state=42)

print('Train SVM')

clf = svm.SVC(decision_function_shape='ovo')