예제 #1
0
    print(
        param[im]
    )  # parameter in form tensor([2.5508, 0.0000, 0.0000, 0.0000, 0.0000, 5.0000])

    image2show = image[im]  # indexing random  one image
    print(image2show.size())  #torch.Size([3, 512, 512])
    plt.imshow((image2show * 0.5 + 0.5).numpy().transpose(1, 2, 0))
    plt.show()
    break  # break here just to show 1 batch of data

#  ------------------------------------------------------------------

# for noise in np.arange(0, 1, 0.1):

noise = 0.0
model = resnet50(cifar=True)  #use pretrained on imagenet if cifar is true
model = model.to(device)  # transfer the neural net onto the GPU
criterion = nn.MSELoss(
)  # define the loss (MSE, Crossentropy, Binarycrossentropy)

#  ------------------------------------------------------------------

all_Train_losses, all_Test_losses = train(model, train_dataloader,
                                          test_dataloader, n_epochs, criterion,
                                          date4File, cubeSetName, batch_size,
                                          fileExtension, device, noise)

#  ------------------------------------------------------------------

torch.save(
    model.state_dict(),
#
#     # print(image[2])
#     print(image.size(), param.size()) #torch.Size([batch, 3, 512, 512]) torch.Size([batch, 6])
#     im =2
#     print(param[im])  # parameter in form tensor([2.5508, 0.0000, 0.0000, 0.0000, 0.0000, 5.0000])
#
#     image2show = image[im]  # indexing random  one image
#     print(image2show.size()) #torch.Size([3, 512, 512])
#     plt.imshow((image2show * 0.5 + 0.5).numpy().transpose(1, 2, 0))
#     plt.show()
#     break  # break here just to show 1 batch of data

#  ------------------------------------------------------------------

model = resnet50(
    cifar=False,
    modelName=modelName)  #train with the saved model from the training script
model = model.to(device)  # transfer the neural net onto the GPU
criterion = nn.MSELoss()

#  ------------------------------------------------------------------

# test the model
parameters, predicted_params, test_losses, al, bl, gl, xl, yl, zl = testRenderResnet(
    model, test_dataloader, criterion, file_name_extension, device, obj_name)

#  ------------------------------------------------------------------
# display computed parameter against ground truth

obj_name = 'rubik_color'
#     plt.show()
#
#     image2show = sil[im]  # indexing random  one image
#     print(image2show.size())  # torch.Size([3, 512, 512])
#     image2show = image2show.numpy()
#     plt.imshow(image2show, cmap='gray')
#     plt.show()
#
#     break  # break here just to show 1 batch of data

#  ------------------------------------------------------------------

# for noise in np.arange(0, 1, 0.1):
noise = 0.0
# model = resnet50(cifar=False, modelName=modelName) #train with the saved model from the training script
model = resnet50(cifar=True) #train with the pretrained parameter from cifar database
# model = resnet50_multCPU(cifar=True)
model = model.to(device)  # transfer the neural net onto the GPU
criterion = nn.MSELoss()  #nn.BCELoss()   #nn.CrossEntropyLoss()  define the loss (MSE, Crossentropy, Binarycrossentropy)
#
#  ------------------------------------------------------------------

train_losses, all_Test_losses = train_render(model, train_dataloader, test_dataloader,
                                        n_epochs, criterion,
                                        date4File, cubeSetName, batch_size, fileExtension, device, obj_name, noise)

#  ------------------------------------------------------------------

torch.save(model.state_dict(), 'models/{}_FinalModel_train_{}_{}batchs_{}epochs_Noise{}_{}_RenderRegr.pth'.format(date4File, cubeSetName, str(batch_size), str(n_epochs), noise*100,fileExtension))
print('parameters saved')