Change all images in training set


I have a convolutional neural network. And I wanted to train it on images from the training set but first they should be wrapped with my function change(tensor, float) that takes in a tensor/image of the form [hight,width,3] and a float.
Batch size =4

loading data

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader =, batch_size=batch_size,
                                          shuffle=True, num_workers=2)

Cnn architecture

for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data  
        #size of inputs [4,3,32,32] 
        #size of labels [4]

        inputs = change(inputs,0.1) <----------------------------

        # zero the parameter gradients

        # forward + backward + optimize
        outputs = net(inputs) #[4, 10]
        loss = criterion(outputs, labels)

        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
            running_loss = 0.0

print('Finished Training')

I am trying to apply the image function change but it gives an object error.
it there a quick way to fix it?

I am using a Julia function but it works completely fine with other objects. Error message:

JULIA: MethodError: no method matching copy(::PyObject)
Closest candidates are:
  copy(!Matched::T) where T<:SHA.SHA3_CTX at /opt/julia-1.7.2/share/julia/stdlib/v1.7/SHA/src/types.jl:213
  copy(!Matched::T) where T<:SHA.SHA2_CTX at /opt/julia-1.7.2/share/julia/stdlib/v1.7/SHA/src/types.jl:212
  copy(!Matched::Number) at /opt/julia-1.7.2/share/julia/base/number.jl:113


I would recommend to put change function to transforms list, so you do data changes on transformation stage.

partial from functools will help you to fix number of arguments, like this:

from functools import partial

def change(input, float):

# Use partial to fix number of params, such that change accepts only input
change_partial = partial(change, float=pass_float_value_here)

# Add change_partial to a list of transforms before or after converting to tensors
transforms = Compose([
    RandomResizedCrop(img_size),  # example
    # Add change_partial here if it operates on PIL Image
    ToTensor(),  # convert to tensor       
    # Add change_partial here if it operates on torch tensors

Answered By – trsvchn

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.