title: How to remove showing renderings in the Cartpole game from OpenAI in Pytorch

body:
My current code is based off of Pytorch's example on their website where they use env.render() to make the next state. That makes the game run very slow and would like it to run much quicker without the renderings. Here is the the class that uses the render function and at the bottom is the full code. Any ideas how I can get this to work?

class CartPoleEnvManager(): #manage cartpole env
    def __init__(self, device):
        self.device = device
        self.env = gym.make('CartPole-v0').unwrapped #unwrapped so we can see other dynamics that there is no access to otherwise
        self.env.reset() #reset to starting state
        self.current_screen = None #screen initialization
        self.done = False #if episode is finished

    def reset(self):
        self.env.reset()
        self.current_screen = None

    def close(self): #close env
        self.env.close()

    def render(self, mode='human'): #render the current state to screen
        return self.env.render(mode)

    def num_actions_available(self): #returns # actions available to agent (2)
        return self.env.action_space.n

    def take_action(self, action):# step returns tuple containing env observation, reward and diagnostic info -- all from taking a certain action
        _, reward, self.done, _ = self.env.step(action.item()) # only reward and done status are of importance
        return torch.tensor([reward], device=self.device)
    #####action is a tensor, action.item() gives a number, what step wants

    def just_starting(self):
        return self.current_screen is None

    def get_state(self): #return to the current state of env in the form of a processed image of the screen
        if self.just_starting() or self.done:
            self.current_screen = self.get_processed_screen() #state = processed image of diff of 2 separate screens
            black_screen = torch.zeros_like(self.current_screen)
            return black_screen
        else:
            s1 = self.current_screen
            s2 = self.get_processed_screen() ####what is get_processed_screen?
            self.current_screen = s2
            return s2 - s1 # this represents a single state

    def get_screen_height(self):
        screen = self.get_processed_screen()
        return screen.shape[2]

    def get_screen_width(self):
        screen = self.get_processed_screen()
        return screen.shape[3]

    def get_processed_screen(self):
        screen = self.env.render(mode='rgb_array').transpose((2, 0, 1)) # PyTorch expects CHW
        screen = self.crop_screen(screen)
        return self.transform_screen_data(screen)

    def crop_screen(self, screen):
        screen_height = screen.shape[1]

        # Strip off top and bottom
        top = int(screen_height * 0.4)
        bottom = int(screen_height * 0.8)
        screen = screen[:, top:bottom, :]
        return screen

    def transform_screen_data(self, screen):
        # Convert to float, rescale, convert to tensor
        screen = np.ascontiguousarray(screen, dtype=np.float32) / 255
        screen = torch.from_numpy(screen)

        # Use torchvision package to compose image transforms
        resize = T.Compose([
            T.ToPILImage()
            ,T.Resize((40,90))
            ,T.ToTensor()
        ])

        return resize(screen).unsqueeze(0).to(self.device) # add a batch dimension (BCHW)

And the full code:

import gym
import math
import random
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from collections import namedtuple
from itertools import count
from PIL import Image
import torch
import torch.nn as nnfr
import torch.optim as optim
import torch.nn.functional as F
import torchvision.transforms as T
import pennylane as qml
import numpy as np
import torch
import torch.nn as nn
from torch.nn.functional import relu, sigmoid

class DQN(nn.Module):
    def __init__(self, img_height, img_width):
        super().__init__()

        self.fc1 = nn.Linear(in_features=img_height*img_width*3, out_features=64)
        self.fc2 = nn.Linear(in_features=64, out_features=48)
        self.fc3 = nn.Linear(in_features=48, out_features=32)
        self.out = nn.Linear(in_features=32, out_features=2)

    def forward(self, b):
        b = b.flatten(start_dim=1)
        #t = F.relu(clayer_out)
        b = F.relu(self.fc1(b))
        b = F.relu(self.fc2(b))
        b = F.relu(self.fc3(b))
        b = self.out(b)
        return b

is_ipython = 'inline' in matplotlib.get_backend()
if is_ipython: from IPython import display

Experience = namedtuple(
    'Experience',
    ('state', 'action', 'next_state', 'reward')
) # use experiences to train network

class ReplayMemory():
    def __init__(self, capacity): # replay memory has set capacity, only need to
        self.capacity = capacity # initialize capacity for a ReplayMemory object
        self.memory = [] #memory will hold the experiences
        self.push_count = 0 #to show how many experiences we've added to memory

    def push(self, experience): # to add experiences in memory
        if len(self.memory) < self.capacity: # ensuring that the length of memory doesn't exceed set capacity
            self.memory.append(experience)
        else:
            self.memory[self.push_count % self.capacity] = experience # if memory greater than capacity,
         # then the new experiences will be put to the front of memory, erasing the
                            # oldest experiences in the memory array
        self.push_count += 1
    def sample(self, batch_size): # returns a randome sample of experiences, to later use for
        return random.sample(self.memory, batch_size) #random.sample(sequence, k), of the sequence, gives k randomly chosen experiences

    def can_provide_sample(self, batch_size): #to sample, batch size needs to be bigger than memory -- this is important at the beginning
        return len(self.memory) >= batch_size

class EpsilonGreedyStrategy(): #explor vs. exploitation
    def __init__(self, start, end, decay):
        self.start = start
        self.end = end
        self.decay = decay

    def get_exploration_rate(self, current_step):  ####this was not explained in the video, woher kommt
        return self.end + self.start*(1/(1+self.decay*current_step))#self.end + (self.start - self.end) * \
            #math.exp(-1. * current_step * self.decay)

class LearningRate():
    def __init__(self, start, end, decay, current_step):
        self.start = start
        self.end = end
        self.decay = decay
        self.current_step = current_step
    def get_learning_rate(self, current_step):
        self.current_step += 1
        return self.end + self.start*(1/(1+self.decay*current_step))

class lr(): # learning rate class needed. Left for possible future use, need to update things beforehand
    def __init__(self, learning_rate):
        self.current_step = 0
        self.learning_rate = learning_rate
    def update_lr(self):
        lrrate = learning_rate.get_learning_rate(self.current_step)
        self.current_step +=1
class Agent():
    def __init__(self, strategy, num_actions, device): # when we later create an agent object, need to get strategy from epsilon, num_actions = how many actions from a given state (2 for this game), device is the device in pytorch for tensor calculations CPU or GPU
        self.current_step = 0 # current step number in the environment
        self.strategy = strategy
        self.num_actions = num_actions
        self.device = device

    def select_action(self, state, policy_net): #policy_net is the policy trained by DQN
        rate = strategy.get_exploration_rate(self.current_step)
        self.current_step += 1

        if rate > random.random():
            action = random.randrange(self.num_actions)
            return torch.tensor([action]).to(self.device) # explore
        else:
            with torch.no_grad(): #turn off gradient tracking
                return policy_net(state).argmax(dim=1).to(self.device) # exploit

class CartPoleEnvManager(): #manage cartpole env
    def __init__(self, device):
        self.device = device
        self.env = gym.make('CartPole-v0').unwrapped #unwrapped so we can see other dynamics that there is no access to otherwise
        self.env.reset() #reset to starting state
        self.current_screen = None #screen initialization
        self.done = False #if episode is finished

    def reset(self):
        self.env.reset()
        self.current_screen = None

    def close(self): #close env
        self.env.close()

    def render(self, mode='human'): #render the current state to screen
        return self.env.render(mode)

    def num_actions_available(self): #returns # actions available to agent (2)
        return self.env.action_space.n

    def take_action(self, action):# step returns tuple containing env observation, reward and diagnostic info -- all from taking a certain action
        _, reward, self.done, _ = self.env.step(action.item()) # only reward and done status are of importance
        return torch.tensor([reward], device=self.device)
    #####action is a tensor, action.item() gives a number, what step wants

    def just_starting(self):
        return self.current_screen is None

    def get_state(self): #return to the current state of env in the form of a processed image of the screen
        if self.just_starting() or self.done:
            self.current_screen = self.get_processed_screen() #state = processed image of diff of 2 separate screens
            black_screen = torch.zeros_like(self.current_screen)
            return black_screen
        else:
            s1 = self.current_screen
            s2 = self.get_processed_screen() ####what is get_processed_screen?
            self.current_screen = s2
            return s2 - s1 # this represents a single state

    def get_screen_height(self):
        screen = self.get_processed_screen()
        return screen.shape[2]

    def get_screen_width(self):
        screen = self.get_processed_screen()
        return screen.shape[3]

    def get_processed_screen(self):
        screen = self.env.render(mode='rgb_array').transpose((2, 0, 1)) # PyTorch expects CHW
        screen = self.crop_screen(screen)
        return self.transform_screen_data(screen)

    def crop_screen(self, screen):
        screen_height = screen.shape[1]

        # Strip off top and bottom
        top = int(screen_height * 0.4)
        bottom = int(screen_height * 0.8)
        screen = screen[:, top:bottom, :]
        return screen

    def transform_screen_data(self, screen):
        # Convert to float, rescale, convert to tensor
        screen = np.ascontiguousarray(screen, dtype=np.float32) / 255
        screen = torch.from_numpy(screen)

        # Use torchvision package to compose image transforms
        resize = T.Compose([
            T.ToPILImage()
            ,T.Resize((40,90))
            ,T.ToTensor()
        ])

        return resize(screen).unsqueeze(0).to(self.device) # add a batch dimension (BCHW)

def plot(values, moving_avg_period):
    plt.figure(2)
    plt.clf()
    plt.title('Training...')
    plt.xlabel('Episode')
    plt.ylabel('Duration')
    plt.plot(values)

    moving_avg = get_moving_average(moving_avg_period, values)
    plt.plot(moving_avg)
    plt.pause(0.001)
    print("Episode", len(values), "\n", \
          moving_avg_period, "episode moving avg:", moving_avg[-1])
    if is_ipython: display.clear_output(wait=True)

def get_moving_average(period, values):
    values = torch.tensor(values, dtype=torch.float)
    if len(values) >= period:
        moving_avg = values.unfold(dimension=0, size=period, step=1) \
            .mean(dim=1).flatten(start_dim=0)
        moving_avg = torch.cat((torch.zeros(period-1), moving_avg))
        return moving_avg.numpy()
    else:
        moving_avg = torch.zeros(len(values))
        return moving_avg.numpy()

def extract_tensors(experiences):
    # Convert batch of Experiences to Experience of batches
    batch = Experience(*zip(*experiences))

    t1 = torch.cat(batch.state)
    t2 = torch.cat(batch.action)
    t3 = torch.cat(batch.reward)
    t4 = torch.cat(batch.next_state)

    return (t1,t2,t3,t4)

class QValues():
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    @staticmethod
    def get_current(policy_net, states, actions):
        return policy_net(states).gather(dim=1, index=actions.unsqueeze(-1))

    @staticmethod
    def get_next(target_net, next_states):
        final_state_locations = next_states.flatten(start_dim=1) \
            .max(dim=1)[0].eq(0).type(torch.bool)
        non_final_state_locations = (final_state_locations == False)
        non_final_states = next_states[non_final_state_locations]
        batch_size = next_states.shape[0]
        values = torch.zeros(batch_size).to(QValues.device)
        values[non_final_state_locations] = target_net(non_final_states).max(dim=1)[0].detach()
        return values

batch_size = 128
gamma = 0.999
eps_start = 1
eps_end = 0.01
eps_decay = 0.0005
target_update = 10
memory_size = 500000
lr_start = 0.01
lr_end = 0.00001
lr_decay = 0.00009
num_episodes = 1000 # run for more episodes for better results

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
em = CartPoleEnvManager(device)
strategy = EpsilonGreedyStrategy(eps_start, eps_end, eps_decay)
agent = Agent(strategy, em.num_actions_available(), device)
memory = ReplayMemory(memory_size)

policy_net = DQN(em.get_screen_height(), em.get_screen_width()).to(device)
target_net = DQN(em.get_screen_height(), em.get_screen_width()).to(device)
target_net.load_state_dict(policy_net.state_dict())

target_net.eval() #tells pytorch that target_net is only used for inference, not training
optimizer = optim.Adam(params=policy_net.parameters(), lr=0.01)

i = 0
episode_durations = []
for episode in range(num_episodes): #iterate over each episode
    em.reset()
    state = em.get_state()

    for timestep in count():
        action = agent.select_action(state, policy_net)
        reward = em.take_action(action)
        next_state = em.get_state()
        memory.push(Experience(state, action, next_state, reward))
        state = next_state
        i = 0
        if memory.can_provide_sample(batch_size):
            scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.9)
            experiences = memory.sample(batch_size)
            states, actions, rewards, next_states = extract_tensors(experiences)

            current_q_values = QValues.get_current(policy_net, states, actions)
            next_q_values = QValues.get_next(target_net, next_states) #will get the max qvalues of the next state, q values of next state are used via next state
            target_q_values = (next_q_values * gamma) + rewards

            loss = F.mse_loss(current_q_values, target_q_values.unsqueeze(1))
            optimizer.zero_grad() # sets the gradiesnt of all weights n biases in policy_net to zero
            loss.backward() #computes gradient of loss with respect to all weights n biases in the policy net
            optimizer.step() # updates the weights n biases with the gradients that were computed form loss.backwards
            scheduler.step()

        if em.done:
            episode_durations.append(timestep)
            plot(episode_durations, 100)
            break
    if episode % target_update == 0:
        target_net.load_state_dict(policy_net.state_dict())

em.close()


title: Applying kernels to separate dimensions in gpytorch

body:
I am translating three-dimensional Gaussian process regression code from GPflow into GPyTorch to take advantage of PyTorch's native scalability for exact GPR.
This example is helpful, but can't find an explanation for how to apply distinct kernels to individual dimensions in a multi-dimensional GPR (e.g. a Matern52 kernel to dimensions [0,1], and a Matern 32 kernel to dimension [2]).
Does GPyTorch support that functionality, and if so how is it implemented/where can I find an example?


title: How to use a neural network (Pytorch- or Tensorflow-based) in Fortran?

body: Python is popular and optimal for neural network development and training. However, many scientific codes are written in the Fortran language. How I can call a trained network in my Fortran program?


import pandas as pd
from ast import literal_eval

from cdqa.utils.filters import filter_paragraphs
from cdqa.utils.download import download_model, download_bnpp_data
from cdqa.pipeline.cdqa_sklearn import QAPipeline

# Download data and models
download_bnpp_data(dir='./data/bnpp_newsroom_v1.1/')
download_model(model='bert-squad_1.1', dir='./models')

# Loading data and filtering / preprocessing the documents
df = pd.read_csv('data/bnpp_newsroom_v1.1/bnpp_newsroom-v1.1.csv', converters={'paragraphs': literal_eval})
df = filter_paragraphs(df)

# Loading QAPipeline with CPU version of BERT Reader pretrained on SQuAD 1.1
cdqa_pipeline = QAPipeline(reader='C:/models/bert_qa.joblib')

# Fitting the retriever to the list of documents in the dataframe
cdqa_pipeline.fit_retriever(X=df)

I was trying to load model but the  cdqa_pipeline = QAPipeline(reader='C:/models/bert_qa.joblib') line throws an error saying AttributeError: module 'transformers.modeling_bert' has no attribute 'gelu'
I was using transformers version 3.5


title: I got IndexError: Target 3 is out of bounds. Error on making model from CNN module in line loss = loss_fn(outputs, labels)

body:
my code is like this and i do everything to found error but it doesnot work
import cv2
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
import torchvision.datasets as datasets
from torch.autograd import Variable
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
#Transformation for image
transform_ori = transforms.Compose([transforms.RandomResizedCrop(64),   #create 64x64 image
                                    transforms.RandomHorizontalFlip(),    #flipping the image horizontally
                                    transforms.ToTensor(),                 #convert the image to a Tensor
                                    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])  #normalize the image

#Load our dataset
train_dataset = datasets.ImageFolder(root = '/content//Data',
                                     transform = transform_ori)

test_dataset = datasets.ImageFolder(root = '/content/Data',
                                    transform = transform_ori)

#Make the dataset iterable
batch_size = 100
train_load = torch.utils.data.DataLoader(dataset = train_dataset,
                                         batch_size = batch_size,
                                         shuffle = True)

batch_size = 100
train_load = torch.utils.data.DataLoader(dataset = train_dataset,
                                         batch_size = batch_size,
                                         shuffle = True)      #Shuffle to create a mixed batches of 100 of cat & dog images

test_load = torch.utils.data.DataLoader(dataset = test_dataset,
                                         batch_size = batch_size,
                                         shuffle = False)

#Show a batch of images
def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.figure(figsize=(20,20))
    plt.imshow(np.transpose(npimg, (1, 2, 0)))

# get some random training images
dataiter = iter(train_load)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))

print("There are {} images in train set".format(len(train_dataset)))
print("There are {} images in train loader".format(len(train_load)))
print("There are {} images in test set".format(len(test_dataset)))
print("There are {} images in test loader".format(len(test_load)))

class CNN(nn.Module):
    def __init__(self):
        super(CNN,self).__init__()

        self.cnn1 = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3,stride=1, padding=1)
        self.batchnorm1 = nn.BatchNorm2d(8)        #Batch normalization
        self.relu = nn.ReLU()                 #RELU Activation
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)   #Maxpooling reduces the size by kernel size. 64/2 = 32

        self.cnn2 = nn.Conv2d(in_channels=8, out_channels=32, kernel_size=5, stride=1, padding=2)
        self.batchnorm2 = nn.BatchNorm2d(32)
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)    #Size now is 32/2 = 16

        #Flatten the feature maps. You have 32 feature mapsfrom cnn2. Each of the feature is of size 16x16 --> 32*16*16 = 8192
        self.fc1 = nn.Linear(in_features=8192, out_features=4000)   #Flattened image is fed into linear NN and reduced to half size
        self.droput = nn.Dropout(p=0.5)                    #Dropout used to reduce overfitting
        self.fc2 = nn.Linear(in_features=4000, out_features=2000)
        self.droput = nn.Dropout(p=0.5)
        self.fc3 = nn.Linear(in_features=2000, out_features=500)
        self.droput = nn.Dropout(p=0.5)
        self.fc4 = nn.Linear(in_features=500, out_features=50)
        self.droput = nn.Dropout(p=0.5)
        self.fc5 = nn.Linear(in_features=50, out_features=2)    #Since there were so many features, I decided to use 45 layers to get output layers. You can increase the kernels in Maxpooling to reduce image further and reduce number of hidden linear layers.

def forward(self,x):
        out = self.cnn1(x)
        out = self.batchnorm1(out)
        out = self.relu(out)
        out = self.maxpool1(out)
        out = self.cnn2(out)
        out = self.batchnorm2(out)
        out = self.relu(out)
        out = self.maxpool2(out)
        #Flattening is done here with .view() -> (batch_size, 32*16*16) = (100, 8192)
        out = out.view(-1,8192)   #-1 will automatically update the batchsize as 100; 8192 flattens 32,16,16
        #Then we forward through our fully connected layer
        out = self.fc1(out)
        out = self.relu(out)
        out = self.droput(out)
        out = self.fc2(out)
        out = self.relu(out)
        out = self.droput(out)
        out = self.fc3(out)
        out = self.relu(out)
        out = self.droput(out)
        out = self.fc4(out)
        out = self.relu(out)
        out = self.droput(out)
        out = self.fc5(out)
        return out

model = CNN()
CUDA = torch.cuda.is_available()
if CUDA:
    model = model.cuda()
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)

import time

num_epochs = 50

#Define the lists to store the results of loss and accuracy
train_loss = []
test_loss = []
train_accuracy = []
test_accuracy = []

#Training
for epoch in range(num_epochs):
    #Reset these below variables to 0 at the begining of every epoch
    start = time.time()
    correct = 0
    iterations = 0
    iter_loss = 0.0

    model.train()                   # Put the network into training mode

    for i, (inputs, labels) in enumerate(train_load):

      # Convert torch tensor to Variable
      Inputs = Variable(inputs)
      Labels = Variable(labels)

      # If we have GPU, shift the data to GPU
      CUDA = torch.cuda.is_available()
      if CUDA:
        print("Cuda is Available!")
        Inputs = Inputs.cuda()
        Labels = Labels.cuda()

      optimizer.zero_grad()
      outputs = model(inputs)

      loss = loss_fn(outputs, labels)
      iter_loss += loss.data[0]       # Accumulate the loss
      loss.backward()                 # Backpropagation
      optimizer.step()                # Update the weights

      # Record the correct predictions for training data
      _, predicted = torch.max(outputs, 1)
      correct += (predicted == labels).sum()
      iterations += 1

    # Record the training loss
    train_loss.append(iter_loss/iterations)
    # Record the training accuracy
    train_accuracy.append((100 * correct / len(train_dataset)))

    #Testing
    loss = 0.0
    correct = 0
    iterations = 0

    model.eval()                    # Put the network into evaluation mode

    for i, (inputs, labels) in enumerate(test_load):

        # Convert torch tensor to Variable
        inputs = Variable(inputs)
        labels = Variable(labels)

        CUDA = torch.cuda.is_available()
        if CUDA:
            inputs = inputs.cuda()
            labels = labels.cuda()

        outputs = model(inputs)
        loss = loss_fn(outputs, labels) # Calculate the loss
        loss += loss.data[0]
        # Record the correct predictions for training data
        _, predicted = torch.max(outputs, 1)
        correct += (predicted == labels).sum()

        iterations += 1

    # Record the Testing loss
    test_loss.append(loss/iterations)
    # Record the Testing accuracy
    test_accuracy.append((100 * correct / len(test_dataset)))
    stop = time.time()

    print ('Epoch {}/{}, Training Loss: {:.3f}, Training Accuracy: {:.3f}, Testing Loss: {:.3f}, Testing Acc: {:.3f}, Time: {}s'
           .format(epoch+1, num_epochs, train_loss[-1], train_accuracy[-1], test_loss[-1], test_accuracy[-1], stop-start))

# Loss
f = plt.figure(figsize=(10, 10))
plt.plot(train_loss, label='Training Loss')
plt.plot(test_loss, label='Testing Loss')
plt.legend()
plt.show()

# Accuracy
f = plt.figure(figsize=(10, 10))
plt.plot(train_accuracy, label='Training Accuracy')
plt.plot(test_accuracy, label='Testing Accuracy')
plt.legend()
plt.show()

#Run this if you want to save the model
torch.save(model.state_dict(),'Cats-Dogs.pth')

#Run this if you want to load the model
model.load_state_dict(torch.load('Cats-Dogs.pth'))

def predict(img_name,model):
    image = cv2.imread(img_name)   #Read the image
    img = Image.fromarray(image)      #Convert the image to an array
    img = transforms_photo(img)     #Apply the transformations
    img = img.view(1,3,64,64)       #Add batch size
    img = Variable(img)
    #Wrap the tensor to a variable

    model.eval()

    if torch.cuda.is_available():
        model = model.cuda()
        img = img.cuda()

    output = model(img)
    print(output)
    print(output.data)
    _, predicted = torch.max(output,1)
    if predicted.item()==0:
        p = 'Cat'
    else:
        p = 'Dog'
    cv2.imshow('Original',image)
    return  p

pred = predict('dataset/single_prediction/cat_or_dog_1.jpg', model)
print("The Predicted Label is {}".format(pred))

#Load the DenseNet
model_conv = torchvision.models.densenet201(pretrained=True)

#Freeze all layers in the network
for param in model_conv.parameters():
    param.requires_grad = False

#Get the number of inputs of the last layer (or number of neurons in the layer preceeding the last layer)
num_ftrs = model_conv.classifier.in_features

#Reconstruct the last layer (output layer) to have only two classes
model_conv.classifier = nn.Linear(num_ftrs, 2)

#Initiate the model on GPU
if torch.cuda.is_available():
    model_conv = model_conv.cuda()

#Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_conv.parameters(), lr=0.001, momentum=0.9) #Try Adam optimizer for better accuracy: optim.Adam(model_conv.parameters(), lr=0.001)

#Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)

train_loss = []
test_loss = []
train_accuracy = []
test_accuracy = []

#Training
import time
num_epochs = 20
for epoch in range (num_epochs):
    start = time.time()
    exp_lr_scheduler.step()
    #Reset the correct to 0 after passing through all the dataset
    correct = 0
    for images,labels in dataloaders['training_set']:
        images = Variable(images)
        labels = Variable(labels)
        if torch.cuda.is_available():
            images = images.cuda()
            labels = labels.cuda()

        optimizer.zero_grad()
        outputs = model_conv(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        _, predicted = torch.max(outputs, 1)
        correct += (predicted == labels).sum()

    train_acc = 100 * correct / dataset_sizes['training_set']
    stop = time.time()
    print ('Epoch [{}/{}], Loss: {:.4f}, Train Accuracy: {}%, Time: {:.2f}s'
            .format(epoch+1, num_epochs, loss.item(), train_acc, stop-start))

   #Testing
   model_conv.eval()
with torch.no_grad():
    correct = 0
    total = 0
    start = time.time()
    for (images, labels) in dataloaders['test_set']:

        images = Variable(images)
        labels = Variable(labels)
        if torch.cuda.is_available():
            images = images.cuda()
            labels = labels.cuda()

        outputs = model_conv(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
    stop = time.time()

    print('Test Accuracy: {:.3f} %, Time: {:.2f}s'.format(100 * correct / total,stop-start))

#Import your trial images and check against the model
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
#Predict your own image
def predict(img_name,model):
    image = cv2.imread(img_name)   #Read the image
    #ret, thresholded = cv2.threshold(image,127,255,cv2.THRESH_BINARY)   #Threshold the image
    img = Image.fromarray(image)      #Convert the image to an array
    img = transforms_photo(img)     #Apply the transformations
    img = img.view(1,3,224,224)       #Add batch size
    img = Variable(img)
   #Wrap the tensor to a variable

    model.eval()

    if torch.cuda.is_available():
        model = model.cuda()
        img = img.cuda()

    output = model(img)
    print(output)
    print(output.data)
    _, predicted = torch.max(output,1)
    if predicted.item()==0:
        p = 'Cat'
    else:
        p = 'Dog'
    cv2.imshow('Original',image)
    return  p

This question already has answers here:
PyTorch NotImplementedError in forward (2 answers)
Your post has been associated with a similar question. If this question doesn’t resolve your question, ask a new one.

Closed 3 days ago.

(Private feedback for you)

here is my code :

import cv2
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
import torchvision.datasets as datasets
from torch.autograd import Variable
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

#Transformation for image
transform_ori = transforms.Compose([transforms.RandomResizedCrop(64),   #create 64x64 image
                                    transforms.RandomHorizontalFlip(),    #flipping the image horizontally
                                    transforms.ToTensor(),                 #convert the image to a Tensor
                                    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])  #normalize the image

#Load our dataset
train_dataset = datasets.ImageFolder(root = '/content//Data',
                                     transform = transform_ori)

test_dataset = datasets.ImageFolder(root = '/content/Data',
                                    transform = transform_ori)

#Make the dataset iterable
batch_size = 100
train_load = torch.utils.data.DataLoader(dataset = train_dataset,
                                         batch_size = batch_size,
                                         shuffle = True)

batch_size = 100
train_load = torch.utils.data.DataLoader(dataset = train_dataset,
                                         batch_size = batch_size,
                                         shuffle = True)      #Shuffle to create a mixed batches of 100 of cat & dog images

test_load = torch.utils.data.DataLoader(dataset = test_dataset,
                                         batch_size = batch_size,
                                         shuffle = False)

#Show a batch of images
def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.figure(figsize=(20,20))
    plt.imshow(np.transpose(npimg, (1, 2, 0)))

# get some random training images
dataiter = iter(train_load)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))

print("There are {} images in train set".format(len(train_dataset)))
print("There are {} images in train loader".format(len(train_load)))
print("There are {} images in test set".format(len(test_dataset)))
print("There are {} images in test loader".format(len(test_load)))

class CNN(nn.Module):
    def __init__(self):
        super(CNN,self).__init__()

        self.cnn1 = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3,stride=1, padding=1)
        self.batchnorm1 = nn.BatchNorm2d(8)        #Batch normalization
        self.relu = nn.ReLU()                 #RELU Activation
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)   #Maxpooling reduces the size by kernel size. 64/2 = 32

        self.cnn2 = nn.Conv2d(in_channels=8, out_channels=32, kernel_size=5, stride=1, padding=2)
        self.batchnorm2 = nn.BatchNorm2d(32)
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)    #Size now is 32/2 = 16

        #Flatten the feature maps. You have 32 feature mapsfrom cnn2. Each of the feature is of size 16x16 --> 32*16*16 = 8192
        self.fc1 = nn.Linear(in_features=8192, out_features=4000)   #Flattened image is fed into linear NN and reduced to half size
        self.droput = nn.Dropout(p=0.5)                    #Dropout used to reduce overfitting
        self.fc2 = nn.Linear(in_features=4000, out_features=2000)
        self.droput = nn.Dropout(p=0.5)
        self.fc3 = nn.Linear(in_features=2000, out_features=500)
        self.droput = nn.Dropout(p=0.5)
        self.fc4 = nn.Linear(in_features=500, out_features=50)
        self.droput = nn.Dropout(p=0.5)
        self.fc5 = nn.Linear(in_features=50, out_features=2)    #Since there were so many features, I decided to use 45 layers to get output layers. You can increase the kernels in Maxpooling to reduce image further and reduce number of hidden linear layers.

def forward(self,x):
        out = self.cnn1(x)
        out = self.batchnorm1(out)
        out = self.relu(out)
        out = self.maxpool1(out)
        out = self.cnn2(out)
        out = self.batchnorm2(out)
        out = self.relu(out)
        out = self.maxpool2(out)
        #Flattening is done here with .view() -> (batch_size, 32*16*16) = (100, 8192)
        out = out.view(-1,8192)   #-1 will automatically update the batchsize as 100; 8192 flattens 32,16,16
        #Then we forward through our fully connected layer
        out = self.fc1(out)
        out = self.relu(out)
        out = self.droput(out)
        out = self.fc2(out)
        out = self.relu(out)
        out = self.droput(out)
        out = self.fc3(out)
        out = self.relu(out)
        out = self.droput(out)
        out = self.fc4(out)
        out = self.relu(out)
        out = self.droput(out)
        out = self.fc5(out)
        return out

model = CNN()
CUDA = torch.cuda.is_available()
if CUDA:
    model = model.cuda()
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)

import time

num_epochs = 50

#Define the lists to store the results of loss and accuracy
train_loss = []
test_loss = []
train_accuracy = []
test_accuracy = []

#Training
for epoch in range(num_epochs):
    #Reset these below variables to 0 at the begining of every epoch
    start = time.time()
    correct = 0
    iterations = 0
    iter_loss = 0.0

    model.train()                   # Put the network into training mode

    for i, (inputs, labels) in enumerate(train_load):

      # Convert torch tensor to Variable
      Inputs = Variable(inputs)
      Labels = Variable(labels)

      # If we have GPU, shift the data to GPU
      CUDA = torch.cuda.is_available()
      if CUDA:
        print("Cuda is Available!")
        Inputs = Inputs.cuda()
        Labels = Labels.cuda()

      optimizer.zero_grad()
      outputs = model(inputs)

      loss = loss_fn(outputs, labels)
      iter_loss += loss.data[0]       # Accumulate the loss
      loss.backward()                 # Backpropagation
      optimizer.step()                # Update the weights

      # Record the correct predictions for training data
      _, predicted = torch.max(outputs, 1)
      correct += (predicted == labels).sum()
      iterations += 1

    # Record the training loss
    train_loss.append(iter_loss/iterations)
    # Record the training accuracy
    train_accuracy.append((100 * correct / len(train_dataset)))

    #Testing
    loss = 0.0
    correct = 0
    iterations = 0

    model.eval()                    # Put the network into evaluation mode

    for i, (inputs, labels) in enumerate(test_load):

        # Convert torch tensor to Variable
        inputs = Variable(inputs)
        labels = Variable(labels)

        CUDA = torch.cuda.is_available()
        if CUDA:
            inputs = inputs.cuda()
            labels = labels.cuda()

        outputs = model(inputs)
        loss = loss_fn(outputs, labels) # Calculate the loss
        loss += loss.data[0]
        # Record the correct predictions for training data
        _, predicted = torch.max(outputs, 1)
        correct += (predicted == labels).sum()

        iterations += 1

    # Record the Testing loss
    test_loss.append(loss/iterations)
    # Record the Testing accuracy
    test_accuracy.append((100 * correct / len(test_dataset)))
    stop = time.time()

    print ('Epoch {}/{}, Training Loss: {:.3f}, Training Accuracy: {:.3f}, Testing Loss: {:.3f}, Testing Acc: {:.3f}, Time: {}s'
           .format(epoch+1, num_epochs, train_loss[-1], train_accuracy[-1], test_loss[-1], test_accuracy[-1], stop-start))

# Loss
f = plt.figure(figsize=(10, 10))
plt.plot(train_loss, label='Training Loss')
plt.plot(test_loss, label='Testing Loss')
plt.legend()
plt.show()

# Accuracy
f = plt.figure(figsize=(10, 10))
plt.plot(train_accuracy, label='Training Accuracy')
plt.plot(test_accuracy, label='Testing Accuracy')
plt.legend()
plt.show()

#Run this if you want to save the model
torch.save(model.state_dict(),'Cats-Dogs.pth')

#Run this if you want to load the model
model.load_state_dict(torch.load('Cats-Dogs.pth'))

def predict(img_name,model):
    image = cv2.imread(img_name)   #Read the image
    img = Image.fromarray(image)      #Convert the image to an array
    img = transforms_photo(img)     #Apply the transformations
    img = img.view(1,3,64,64)       #Add batch size
    img = Variable(img)
    #Wrap the tensor to a variable

    model.eval()

    if torch.cuda.is_available():
        model = model.cuda()
        img = img.cuda()

    output = model(img)
    print(output)
    print(output.data)
    _, predicted = torch.max(output,1)
    if predicted.item()==0:
        p = 'Cat'
    else:
        p = 'Dog'
    cv2.imshow('Original',image)
    return  p

pred = predict('dataset/single_prediction/cat_or_dog_1.jpg', model)
print("The Predicted Label is {}".format(pred))

#Load the DenseNet
model_conv = torchvision.models.densenet201(pretrained=True)

#Freeze all layers in the network
for param in model_conv.parameters():
    param.requires_grad = False

#Get the number of inputs of the last layer (or number of neurons in the layer preceeding the last layer)
num_ftrs = model_conv.classifier.in_features

#Reconstruct the last layer (output layer) to have only two classes
model_conv.classifier = nn.Linear(num_ftrs, 2)

#Initiate the model on GPU
if torch.cuda.is_available():
    model_conv = model_conv.cuda()

#Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_conv.parameters(), lr=0.001, momentum=0.9) #Try Adam optimizer for better accuracy: optim.Adam(model_conv.parameters(), lr=0.001)

#Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)

train_loss = []
test_loss = []
train_accuracy = []
test_accuracy = []

#Training
import time
num_epochs = 20
for epoch in range (num_epochs):
    start = time.time()
    exp_lr_scheduler.step()
    #Reset the correct to 0 after passing through all the dataset
    correct = 0
    for images,labels in dataloaders['training_set']:
        images = Variable(images)
        labels = Variable(labels)
        if torch.cuda.is_available():
            images = images.cuda()
            labels = labels.cuda()

        optimizer.zero_grad()
        outputs = model_conv(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        _, predicted = torch.max(outputs, 1)
        correct += (predicted == labels).sum()

    train_acc = 100 * correct / dataset_sizes['training_set']
    stop = time.time()
    print ('Epoch [{}/{}], Loss: {:.4f}, Train Accuracy: {}%, Time: {:.2f}s'
            .format(epoch+1, num_epochs, loss.item(), train_acc, stop-start))

   #Testing
   model_conv.eval()
with torch.no_grad():
    correct = 0
    total = 0
    start = time.time()
    for (images, labels) in dataloaders['test_set']:

        images = Variable(images)
        labels = Variable(labels)
        if torch.cuda.is_available():
            images = images.cuda()
            labels = labels.cuda()

        outputs = model_conv(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
    stop = time.time()

    print('Test Accuracy: {:.3f} %, Time: {:.2f}s'.format(100 * correct / total,stop-start))

#Import your trial images and check against the model
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
#Predict your own image
def predict(img_name,model):
    image = cv2.imread(img_name)   #Read the image
    #ret, thresholded = cv2.threshold(image,127,255,cv2.THRESH_BINARY)   #Threshold the image
    img = Image.fromarray(image)      #Convert the image to an array
    img = transforms_photo(img)     #Apply the transformations
    img = img.view(1,3,224,224)       #Add batch size
    img = Variable(img)
   #Wrap the tensor to a variable

    model.eval()

    if torch.cuda.is_available():
        model = model.cuda()
        img = img.cuda()

    output = model(img)
    print(output)
    print(output.data)
    _, predicted = torch.max(output,1)
    if predicted.item()==0:
        p = 'Cat'
    else:
        p = 'Dog'
    cv2.imshow('Original',image)
    return  p

and i got this :
I test some part of my code but it seems everything is ok aanybody can help?
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   2262                          .format(input.size(0), target.size(0)))
   2263     if dim == 2:
-> 2264         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   2265     elif dim == 4:
   2266         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

IndexError: Target 3 is out of bounds.

i found nothing in searching the web.and also i found nothing in stack,i read its doc and i found nothing


title: error in backward function

body:
I already defined a loss function in pytorch, but there is an error that I could not find solution.  Here is my code:

<code>
class cust_loss(torch.nn.Module):
    def __init__(self):
        super(cust_loss, self).__init__()

def forward(self, input, target):
    predicted_labels = torch.max(input, 1)[1]
    minus = torch.max(input, 1)[1] - target
    cust_distance = torch.sum(minus*minus).type(torch.FloatTensor)/predicted_labels.size()[0]
    return cust_distance

######## within main function ######

criterion = cust_loss()#nn.CrossEntropyLoss()
Optimizer = optim.SGD(filter(lambda p: p.requires_grad, model_conv.parameters()), lr=1e-3, momentum=0.9)

loss = criterion(inputs, labels)
loss.backward()

Unfortunately, I got this error:

Traceback (most recent call last):
  File "/home/morteza/PycharmProjects/transfer_learning/test_SkinDetection.py", line 250, in <module>
    main(True)
  File "/home/morteza/PycharmProjects/transfer_learning/test_SkinDetection.py", line 130, in main
    loss.backward()
  File "/home/morteza/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.py", line 156, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)
  File "/home/morteza/anaconda3/lib/python3.6/site-packages/torch/autograd/__init__.py", line 98, in backward
    variables, grad_variables, retain_graph)
  File "/home/morteza/anaconda3/lib/python3.6/site-packages/torch/autograd/function.py", line 91, in apply
    return self._forward_cls.backward(self, *args)
  File "/home/morteza/anaconda3/lib/python3.6/site-packages/torch/autograd/_functions/basic_ops.py", line 38, in backward
    return maybe_unexpand(grad_output, ctx.a_size), maybe_unexpand_or_view(grad_output.neg(), ctx.b_size), None
  File "/home/morteza/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.py", line 381, in neg
    return Negate.apply(self)
  File "/home/morteza/anaconda3/lib/python3.6/site-packages/torch/autograd/_functions/basic_ops.py", line 224, in forward
    return i.neg()
 AttributeError: 'torch.LongTensor' object has no attribute 'neg'

I could not solve it. I traced the code and compared it with a  code that is error free, but I could not solve it. Moreover, I defined my inputs and labels as Variable with "requires_grad=True" parameter.
Please guide me how to solve it.
Thank you.


title: Weights to Crossentropy loss

body:
Let's say I have two classes, class A (100 samples) and class B (100 samples) in training but while testing the class A has (1000 samples) and class B has (100 samples).
How am I supposed to calculate and use weights for weighted CrossEntropy Loss. I am confused if it should be 0.5, 0.5 or not. How can I represent the true distribution?


title: Selecting second dim of tensor using an index tensor

body:
I have a 2D tensor and an index tensor.  The 2D tensor has a batch dimension, and a dimension with 3 values.  I have an index tensor that selects exactly 1 element of the 3 values.  What is the "best" way to product a slice containing just the elements in the index tensor?

t = torch.tensor([[1,2,3], [4,5,6], [7,8,9]])
t = tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])

i = torch.tensor([0,0,1], dtype=torch.int64)
tensor([0, 0, 1])

Expected output...

tensor([1, 4, 8])


title: PyTorch: inconsistent pretrained VGG output

body:
When loading a pretrained VGG network with the torchvision.models module and using it to classify an arbitrary RGB image, the network's output differs noticeably from invocation to invocation. Why does this happen? From my understanding no part of the VGG forward pass should be non-deterministic.

Here's an MCVE:

import torch
from torchvision.models import vgg16

vgg = vgg16(pretrained=True)

img = torch.randn(1, 3, 256, 256)

torch.all(torch.eq(vgg(img), vgg(img))) # result is 0, but why?


title: how to deploy pytorch based GAN model to android

body:
I have trained and tested the pytorch based GAN model and now I want to deploy it to Android.
I have read about converting cnn model to onnx format and then to cafe2
but I don't know how to use it if there is more than one model i.e. generator, discriminator, encoder-decoder models.


title: What is the difference between feature and classifier?

body:
I saw code like this

self.feature = model_func()
if loss_type == 'softmax':
    self.classifier = nn.Linear(self.feature.final_feat_dim, num_class)
    self.classifier.bias.data.fill_(0)
elif loss_type == 'dist': #Baseline ++
    self.classifier = backbone.distLinear(self.feature.final_feat_dim, num_class)

where model_func is a ConvNet 4/6 or ResNet 10/18/34/101

What here is classifier?

I know that in neural networks we have parameters that we learn, buffers that are used to store something that gets updated during training, activations that are the results after each layer.

Is feature same as activation, and what is a classifier, where is the end of features, and the beginning of classifier in a neural network? Is the result of a classifier also activation?


title: Multiplying and powering python float and pytorch integer

body:
Why does a python float multiplied by a torch.long gives a torch.float but powering a float by a torch.long gives a torch.long?

>>> a = 0.9
>>> b = torch.tensor(2, dtype=torch.long)

>>> foo = a * b
>>> print(foo, foo.dtype)
tensor(1.8000) torch.float32

>>> bar = a ** b
>>> print(bar, bar.dtype)
tensor(0) torch.int64


title: Missing/unexpected keys in resnet50 with pytorch

body:
I get the following errors and no idea why:

Missing key(s) in state_dict: "layer2.0.layer.0.inplace.0.weight", "layer2.0.layer.0.inplace.0.bias",...

Unexpected key(s) in state_dict: "layer2.0.layer.0.0.weight", "layer2.0.layer.0.0.bias",...

The channel sizes I set seem to be what I wanted. however, I don't see where the mistake is

import torch.nn as nn

#1x1 convolution
def conv1x1(in_channels: object, out_channels: object, stride: object, padding: object) -> object:

    model = nn.Sequential(
        nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, padding=padding),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )
    return model

# 3x3 convolution
def conv3x3(in_channels: object, out_channels: object, stride: object, padding: object) -> object:
    model = nn.Sequential(
        nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=padding),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )
    return model

class ResidualBlock(nn.Module):
    def __init__(self, in_channels, middle_channels, out_channels, downsample=False):
        super(ResidualBlock, self).__init__()
        self.downsample = downsample

        if self.downsample:
            self.layer = nn.Sequential(
                nn.ReLU(conv1x1(in_channels, middle_channels,1,0)),
                nn.ReLU(conv3x3(middle_channels, middle_channels,1,0)),
                nn.ReLU(conv1x1(middle_channels,out_channels,1,0))
            )
            self.downsize = conv1x1(in_channels, out_channels, 2, 0)

        else:
            self.layer = nn.Sequential(
                nn.ReLU(conv1x1(in_channels,middle_channels,2,0)),
                nn.ReLU(conv3x3(middle_channels,middle_channels,2,0)),
                nn.ReLU(conv1x1(middle_channels,out_channels,2,0))
            )
            self.make_equal_channel = conv1x1(in_channels, out_channels, 1, 0)

    def forward(self, x):
        if self.downsample:
            out = self.layer(x)
            x = self.downsize(x)
            return out + x
        else:
            out = self.layer(x)
            if x.size() is not out.size():
                x = self.make_equal_channel(x)
            return out + x

class ResNet50_layer4(nn.Module):
    def __init__(self, num_classes= 10 ): # Hint : How many classes in Cifar-10 dataset?
        super(ResNet50_layer4, self).__init__()
        self.layer1 = nn.Sequential(
            #in_channels, out_channels, kernel_size, stride, padding
            nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3),
                # Hint : Through this conv-layer, the input image size is halved.
                #        Consider stride, kernel size, padding and input & output channel sizes.
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=0)
        )
        self.layer2 = nn.Sequential(
            #in_channels, middle_channels, out_channels, downsample=False
            ResidualBlock(in_channels=64, middle_channels=64, out_channels=256, downsample=False),
            ResidualBlock(in_channels=256, middle_channels=64, out_channels=256, downsample=False),
            ResidualBlock(in_channels=256, middle_channels=64,out_channels=256, downsample=True)
        )
        self.layer3 = nn.Sequential(
            ResidualBlock(in_channels=256, middle_channels=128, out_channels=512, downsample=False),
            ResidualBlock(in_channels=512, middle_channels=128, out_channels=512, downsample=False),
            ResidualBlock(in_channels=512, middle_channels=128, out_channels=512, downsample=False),
            ResidualBlock(in_channels=512, middle_channels=128, out_channels=512, downsample=True)
        )
        self.layer4 = nn.Sequential(
            ResidualBlock(in_channels=512, middle_channels=256, out_channels=1024, downsample=False),
            ResidualBlock(in_channels=1024, middle_channels=256, out_channels=1024, downsample=False),
            ResidualBlock(in_channels=1024, middle_channels=256, out_channels=1024, downsample=False),
            ResidualBlock(in_channels=1024, middle_channels=256, out_channels=1024, downsample=False),
            ResidualBlock(in_channels=1024, middle_channels=256, out_channels=1024, downsample=False),
            ResidualBlock(in_channels=1024, middle_channels=256, out_channels=1024, downsample=False)

        )

        self.fc = nn.Linear(1024, 10)
        self.avgpool = nn.AvgPool2d(7, stride=1)

        for m in self.modules():
            if isinstance(m, nn.Linear):
                nn.init.xavier_uniform_(m.weight.data)
            elif isinstance(m, nn.Conv2d):
                nn.init.xavier_uniform_(m.weight.data)

    def forward(self, x):

        out = self.layer1(x)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)
        out = self.avgpool(out)
        out = out.view(out.size()[0], -1)
        out = self.fc(out)

        return out


title: Get Training error if setting CUDA VISIBLE DEVICE

body: I am using PyTorch and Cuda 10.1.  If I set CUDA VISIBLE DEVICE in training, the loss is always NAN, if I don't set CUDA VISIBLE DEVICE, everything is working well. Does anyone know what's the problem is?


title: Does layer normalization delay training?

body:
You can find my code here with the results:

https://github.com/shwe87/tfm-asr/blob/master/ASR-Spanish-Bi-RNN-17062020.ipynb

I tested two simple models for ASR in Spanish:

Model 1:

- Layer Normalization
- Bi-directional GRU
- Dropout
- Fully Connected layer
- Dropout
- Fully Connected layer as a classifier (classifies one of the alphabet chars)

Model 2:

  - Conv Layer 1
  - Conv Layer 2
  - Fully Connected
  - Dropout
  - Bidirectional GRU
  - Fully connected layer as a classifier

I tried with 30 epochs because I have limited resources of GPU.
The validation and training loss for these two models:

Model 1 performed not so good as expected.
Model 2 worked too well, after 20 epochs, it started overfitting (please see the graph in the notebook results) and in the output, I could actually see some words creating which seems like the labels. Although it is overfitting, it still needs training because it doesn't predict the total outcome. For start, I am happy with this model.

I tested a third complex Model.
You can find it here with the results output:

https://github.com/shwe87/tfm-asr/blob/master/ASR-DNN.ipynb

Model 3:

  - Layer Normalization
  - RELU
  - Bidirectional GRU
  - Dropout
  - Stack this 10 times more.

The valid loss and training loss for this model:

I tested this on 30 epochs and there were no good results, actually, all the predictions were blank...

Is this because this complex model needs more epochs for training?

Update:

I modified the model by adding 2 convolutional layer before the stacked GRU and the model seems to have improved.

I see that in the first model and the third model I applied layer normalization and both's prediction seems to be very bad....Does layer normalization makes the learning delay? But according to papers like:
https://www.arxiv-vanity.com/papers/1607.06450/ layer normalization speeds up the training and also helps in speeding the training loss. So, I am really confused. I have limited resources of GPU and I am not sure if I should go for another try without layer normalization.......


title: Why do I get the error 'The ordinal 242 could not be located in the dynamic link library' when trying to import torch?

body:
I am currently encountering some issues when trying to import PyTorch on my computer. I am working from my own local Windows laptop (which doesn't have any GPU) and installed Python 3.6 from python.org. I don't have the Anaconda distribution and usually install any new package by opening a Windows Command Prompt and using this command: pip install package.
Usually that's enough for me to be able to use the package right away (either via a Jupyter Notebook or by writing and running jupya .py script in Sublime Text). But PyTorch seem to be a little less straight forward.
I followed the installation instruction copied below (from the PyTorch website):

pip
No CUDA
To install PyTorch via pip, and do not have a CUDA-capable system or do not require CUDA, in the above selector, choose OS: Windows,
Package: Pip and CUDA: None. Then, run the command that is presented
to you.

I opened my Windows Command Prompt and simply ran this command:
pip install torch==1.5.1+cpu torchvision==0.6.1+cpu -f https://download.pytorch.org/whl/torch_stable.html

The installation was successful and no error showed up during the process.
Now, when I open a Jupyter Notebook and run the command: import torch
I get the following error:
python.exe - Ordinal Not Found

The ordinal 242 could not be located in the dynamic link library

c:\users\bdour\appdata\local\programs\python\python36\lib\site-packages\torch\lib\torch_cpu.dll

I checked and the torch_cpu.dll file DOES exist at the path mentioned in the error.
I tried to understand what that error meant but could not find much help. It seems like many people have issues with PyTorch, often due to some issues with their conda environment, but I am not using any environment. I am just trying to import and run the package locally.
And like I mentioned above, I usually encounter no problem with any other library when simply using a pip install command.
Does anyone know where that error is coming from and how to fix it?
Thank you in advance for your time and help.


title: DataLoader create dataset with pytorch

body:
I have a folder with subfolders (classes), with images inside each subfolder.
data
  |_ classe1
        |_ image1
        |_ image2
  |_ classe2
        |_ ...

My goal is to create a dataset (train + test set) to train my model with pytorch resnet.
I have a error, i dont know how to solve it because i don't really understand the DataLoader structure, so i tried this:
I have this:
dataset = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['data']}

batch_size = 32
validation_split = .3
shuffle_dataset = True
random_seed= 42

# Creating data indices for training and validation splits:
dataset_size = len(dataset)
indices = list(range(dataset_size))
split = int(np.floor(validation_split * dataset_size))
if shuffle_dataset :
    np.random.seed(random_seed)
    np.random.shuffle(indices)
train_indices, val_indices = indices[split:], indices[:split]

# Creating PT data samplers and loaders:
train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)

train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                           sampler=train_sampler)
validation_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                                sampler=valid_sampler)

dataloaders_dict = {'train': train_loader, 'val': validation_loader}

But when i try to run my model, i have this error:
Epoch 0/99
----------
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-79-8c30eb5e6a01> in <module>()
      3
      4 # Train and evaluate
----> 5 model_ft, hist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs, is_inception=False)

4 frames
<ipython-input-56-9421c2d39473> in train_model(model, dataloaders, criterion, optimizer, num_epochs, is_inception)
     22
     23             # Iterate over data.
---> 24             for inputs, labels in dataloaders[phase]:
     25                 inputs = inputs.to(device)
     26                 labels = labels.to(device)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    361
    362     def __next__(self):
--> 363         data = self._next_data()
    364         self._num_yielded += 1
    365         if self._dataset_kind == _DatasetKind.Iterable and \

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in _next_data(self)
    401     def _next_data(self):
    402         index = self._next_index()  # may raise StopIteration
--> 403         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    404         if self._pin_memory:
    405             data = _utils.pin_memory.pin_memory(data)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

KeyError: 0

Any suggestions? Any errors detected?


title: Model modification in Pytorch

body: I want to change weights in a certain kernels in a my saved CNN model. How can I change the values in specific kernels and save to a new model?


title: What does the & operator do in PyTorch and why does it change the shape?

body:
I have code that contains x and y, both of the type torch.autograd.variable.Variable. Their shape is

torch.Size([30, 1, 9])
torch.Size([1, 9, 9])

What I don't understand is, why the following results in a different size/shape

z = x & y
print(z.shape)

which outputs

torch.Size([30, 9, 9])

Why is the shape of z 30*9*9, after x & y? The shape of x is 30*1*9, and the shape of y is 1*9*9, what does & do in x & y?


title: Best practices for exploration/exploitation in Reinforcement Learning

body:
My question follows my examination of the code in the PyTorch DQN tutorial, but then refers to Reinforcement Learning in general: what are the best practices for optimal exploration/exploitation in reinforcement learning?

In the DQN tutorial, the steps_done variable is a global variable, and the EPS_DECAY = 200. This means that: after 128 steps, the epsilon threshold = 0.500; after 889 steps, the epsilon threshold = 0.0600; and after 1500 steps, the epsilon threshold = 0.05047.

This might work for the CartPole problem featured in the tutorial – where the early episodes might be very short and the task fairly simple – but what about on more complex problems in which far more exploration is required? For example, if we had a problem with 40,000 episodes, each of which had 10,000 timesteps, how would we set up the epsilon greedy exploration policy? Is there some rule of thumb that’s used in RL work?

Thank you in advance for any help.


title: Runtime Error during training with CUDA: Edge-Conditioned Convolution on Graphs

body:
I am relatively new to Python and currently trying to use CUDA within a specific neural net: Edge-Conditioned Convolution on Graphs, code can be found here https://github.com/mys007/ecc

I know that there are several questions like mine, but i wasn't able to solve my problem with them.

I would like to train a dataset with CUDA, but the process stops during the training of a (random) epoch with the following error:

File "./main.py", line 317, in <module>
main()
File "./main.py", line 219, in main
acc_train, loss, t_loader, t_trainer = train(epoch)
File "./main.py", line 150, in train
outputs = model(inputs)
File "/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "/workspace/ECC_Test/models.py", line 105, in forward
input = module(input)
File "/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "/workspace/ECC_Test/ecc/GraphConvModule.py", line 173, in forward
return GraphConvFunction(self._in_channels, self._out_channels, idxn, idxe, degs, degs_gpu, self._edge_mem_limit)(input, weights)
File "/workspace/ECC_Test/ecc/GraphConvModule.py", line 69, in forward
cuda_kernels.conv_aggregate_fw(output.narrow(0,startd,numd), products.view(-1,self._out_channels), self._degs_gpu.narrow(0,startd,numd))
File "/workspace/ECC_Test/ecc/cuda_kernels.py", line 122, in conv_aggregate_fw
block=(CUDA_NUM_THREADS,1,1), grid=(GET_BLOCKS(w),n//blockDimY+1,1), stream=stream)
File "cupy/cuda/function.pyx", line 148, in cupy.cuda.function.Function.__call__
File "cupy/cuda/function.pyx", line 130, in cupy.cuda.function._launch
File "cupy/cuda/driver.pyx", line 228, in cupy.cuda.driver.launchKernel
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered

Traceback (most recent call last):
File "cupy/cuda/driver.pyx", line 192, in cupy.cuda.driver.moduleUnload
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
Exception ignored in: 'cupy.cuda.function.Module.__dealloc__'
Traceback (most recent call last):
File "cupy/cuda/driver.pyx", line 192, in cupy.cuda.driver.moduleUnload
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
Traceback (most recent call last):
File "cupy/cuda/driver.pyx", line 192, in cupy.cuda.driver.moduleUnload
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
Exception ignored in: 'cupy.cuda.function.Module.__dealloc__'
Traceback (most recent call last):
File "cupy/cuda/driver.pyx", line 192, in cupy.cuda.driver.moduleUnload
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
Traceback (most recent call last):
File "cupy/cuda/driver.pyx", line 192, in cupy.cuda.driver.moduleUnload
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
Exception ignored in: 'cupy.cuda.function.Module.__dealloc__'
Traceback (most recent call last):
File "cupy/cuda/driver.pyx", line 192, in cupy.cuda.driver.moduleUnload
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
Traceback (most recent call last):
File "cupy/cuda/driver.pyx", line 192, in cupy.cuda.driver.moduleUnload
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
Exception ignored in: 'cupy.cuda.function.Module.__dealloc__'
Traceback (most recent call last):
File "cupy/cuda/driver.pyx", line 192, in cupy.cuda.driver.moduleUnload
File "cupy/cuda/driver.pyx", line 81, in cupy.cuda.driver.check_status
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered

Traceback is with "CUDA_LAUNCH_BLOCKING=1"

Switching to CPU and deactivating CUDA works fine.

I am using SSH to access a Server with 4 Nvidia Tesla V100 32GB with driver version 410.104. CUDA 10.1 and Python 3.6.8 is installed.

Currently Pytorch is 1.1. Is the higher PyTorch version causing problems combined with CUDA 10.1? Or am I running out of memory on the GPU?


title: tf.function property in pytorch

body:
I'm a beginner in pytorch, and I have some functions that are needed to implement in network.

My question is: is there any way like tf.function, or should I use "class(nn.Module)" with variable?

For example, let X be a 10x2 matrix . In pseudo-code:

a = Variable(1.0)
b = Variable(1.0)
Y = a*X[:,0]**2 + b*X[:,1]


title: Problem with creating labels from .csv -file

body:
I’m trying to do a face detection model with jupyter Notebook and encountering some difficulties when trying to write a .csv file with the file names and labels.

Here are the functions I use:

##Attaching label to correct file names

item_list = glob.glob('data/dest/celeba/faces/*/*.jpg'.  )

item_df = pd.DataFrame({'image_name':pd.Series(item_list).apply(lambda x: '/'.join(x.split('/')[-2]))})

istem_df['image_id'] = item_df.image_name.apply(lambda x: x.split('/')[1])

## Creating final label set

label_df = pd.read_csv('data/dest/celeba/labels.csv')

label_df = label_df.merge(item_df, on='image_id', how='inner')

label_df.rename(columns={'label':'tags'}, inplace=True)

label_df.loc[:,['image_name','tags']].to_csv('data/dest/celeba/labels1.csv', index=False)

So the “labels.csv” is like it should be, I think. Here’s a couple of lines from the start of the file:

,image_id,label
0,000001.jpg,Arched_Eyebrows Attractive Brown_Hair Heavy_Makeup High_Cheekbones Mouth_Slightly_Open No_Beard Pointy_Nose Smiling Straight_Hair Wearing_Earrings Wearing_Lipstick Young
1,000002.jpg,Bags_Under_Eyes Big_Nose Brown_Hair High_Cheekbones Mouth_Slightly_Open No_Beard Smiling Young

This should be written again with “tags”. But the labels1.csv looks like this:

image_name,tags

Otherwise it’s empty. When I call print(data.c,'\n',data.classes), it prints 0 [].

Can anyone help me to explain what I'm doing wrong?

Edit. I'm editing this to here to avoid chatting. So I've edited my code according to the help I've recieved and this is what it looks like now:

## Attachhing label to correct file names
item_list = glob.glob('data/dest/celeba/faces/*/*.jpg')
item_df = pd.DataFrame({'image_name':pd.Series(item_list).apply(lambda x: '/'.join(x.split('/')[-2]))})
item_df['image_id'] = item_df.image_name.apply(lambda x: x.split('/')[1])

## Creating final label set
label_df = pd.read_csv('data/dest/celeba/labels.csv')
label_df = label_df.merge(item_df, left_on='image_id', right_on='image_id')
label_df.rename(columns={'label':'tags'}, inplace=True)
label_df.loc[:,['image_name','tags']].to_csv('data/dest/celeba/faces/labels.csv', index=False)

This still gives me an empty "labels1.csv" -file with just the headers: image_name,tags.


title: How can I implement basic question answering with hugging-face?

body:
I have:

from transformers import XLNetTokenizer, XLNetForQuestionAnswering
import torch

tokenizer =  XLNetTokenizer.from_pretrained('xlnet-base-cased')
model = XLNetForQuestionAnswering.from_pretrained('xlnet-base-cased')

input_ids = torch.tensor(tokenizer.encode("What is my name?", add_special_tokens=True)).unsqueeze(0)  # Batch size 1
start_positions = torch.tensor([1])
end_positions = torch.tensor([3])
outputs = model(input_ids, start_positions=start_positions, end_positions=end_positions)
loss = outputs[0]

print(outputs)
print(loss)

As per the docs. This does something giving:

(tensor(2.3008, grad_fn=<DivBackward0>),)
tensor(2.3008, grad_fn=<DivBackward0>)

However, I want an actual answer if possible?


title: “AttributeError: classificadorFinal' object has no attribute 'log_softmax” when trying to train a neural network using pytorch

body:
I'm learning to use pytorch and I got an error that won't let me continue programming.

My code:

import torch.nn as nn
from skorch import NeuralNetClassifier #integracao com sklearn
from sklearn.model_selection import cross_val_score,GridSearchCV
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
import torch
import torch.nn.functional as F
from torch import nn,optim

class classificadorFinal(nn.Module):
    def __init__(self, activation=F.tanh, neurons=16, initializer=torch.nn.init.uniform_, dropout=0.3):
        ##from melhores_parametros
        super().__init__()
        self.dense0 = nn.Linear(4, neurons)
        initializer(self.dense0.weight)
        self.activation0 = activation
        self.dense1 = nn.Linear(neurons, neurons)
        initializer(self.dense1.weight)
        self.activation1 = activation
        self.dense2 = nn.Linear(neurons, 3)

        self.dropout = nn.Dropout(dropout)

    def forward(self, X):
        X = self.dense0(X)
        X = self.activation0(X)
        X = self.dropout(X)
        X = self.dense1(X)
        X = self.activation1(X)
        X = self.dropout(X)
        X = self.dense2(X)
        return X

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(classificador.parameters(), lr = 0.001, weight_decay = 0.0001)

#treino
for epoch in range(200):##from melhores_parametros

    running_loss = 0.
    running_accuracy = 0.

    for data in train_loader:
        inputs, labels = data

        optimizer.zero_grad()

        outputs = classificadorFinal(inputs)

        loss = criterion(outputs, labels)###erro
        loss.backward()

        optimizer.step()

        running_loss += loss.item()

        ps = F.softmax(outputs)

        top_p, top_class = ps.topk(k = 1, dim = 1)

        equals = top_class == labels.view(*top_class.shape)

        running_accuracy += torch.mean(equals.type(torch.float))

    print('Época {:3d}: perda {:3.5f} - precisão {:3.5f}'.format(epoch + 1, running_loss/len(train_loader), running_accuracy/len(train_loader)))

The error occurs exactly on loss = criterion(outputs, labels):

AttributeError: 'classificadorFinal' object has no attribute 'log_softmax'

I found out this error is well known, but I did not understand the proposed solution:

disable aux_logits when the model is created  aux_logits=False.

A little help, please!


title: How to gradually train a model on transformers library?

body:
I have seen this tutorial on how to train a BERT model from scratch on Hugging Face Transformers library.

I'm trying to train a GPT-2 model on 1.5 GB data on Google Colab. I load up all the data using this code:

dataset = LineByLineTextDataset(
    tokenizer=tokenizer,
    file_path="./my-1.5gb-large-file.txt",
    block_size=128,
)

The file gets too large, training fails because of limited memory in the GPU. Is there a way I could train my GPT-2 model gradually by splitting the dataset?


title: How to access weight and L2 norm of conv layers in a CNN in Pytorch?

body: Are there PyTorch functions to access those?


title: Problem with pytorch stack function with the labels of a set of images

body:
I am trying to do run the following code https://github.com/kamenbliznashki/generative_models/blob/master/ssvae.py#L174
. Unfortunately I am encoutering a few problems (line 315,316).
More specifically I have a list of tensor images for example:
[test_dataloader.dataset[i][0] for i in [0,1,2]]

That I want to stack
torch.stack([test_dataloader.dataset[i][0] for i in [0,1,2]], dim=0)
#tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]]],

Everything works perfectly fine in this case. But I also have a label with those images and the stack function is not working anymore. I guess the reason is that test_dataloader.dataset[i][1] for i in [0,1,2] returns a int for every images but pytorch doesn't seem to like it. I think there was an update in pytorch since the guy pushed his code, it used to work in december 2018.
torch.stack([test_dataloader.dataset[i][1] for i in [0,1,2]], dim=0).to(args.device)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-63-ac227397e18c> in <module>
----> 1 torch.stack([test_dataloader.dataset[i][1] for i in [0,1,2]], dim=0).to(args.device)

TypeError: expected Tensor as element 0 in argument 0, but got int

I have tried
torch.stack(torch.FloatTensor([test_dataloader.dataset[i][1] for i in [0,1,2]]), dim=0)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-43-7c4b1dd41c0f> in <module>
----> 1 torch.stack([torch.FloatTensor(test_dataloader.dataset[i][1]) for i in [0,1,2]], dim=0)

RuntimeError: stack expects each tensor to be equal size, but got [7] at entry 0 and [2] at entry 1

I have also tried, where my end goal is to one_hot those values:
def one_hot(x, label_size):
    out = torch.zeros(len(x), label_size).to(x.device)
    out[torch.arange(len(x)), x.squeeze()] = 1
    return out

y = torch.FloatTensor([test_dataloader.dataset[i][1] for i in [0,1,2]])
# > tensor([7., 2., 1.])

y = one_hot(y, args.y_dim)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
 <ipython-input-60-2357ef4d4795> in <module>
 ----> 1 y = one_hot(y, args.y_dim)

<ipython-input-16-cf3479f01a91> in one_hot(x, label_size)
 67 def one_hot(x, label_size):
 68     out = torch.zeros(len(x), label_size).to(x.device)
---> 69     out[torch.arange(len(x)), x.squeeze()] = 1
 70     return out

IndexError: tensors used as indices must be long, byte or bool tensors


title: Invoking SageMaker Endpoint for PyTorch Model

body:
I'm trying to call my SageMaker model endpoint both from Postman and the AWS CLI. The endpoint's status is "in service" but whenever I try to call it it gives me an error. When I try to use the predict function in the SageMaker notebook and provide it a numpy array (ex. np.array([1,2,3,4])), it successfully gives me an output. I'm unsure what I'm doing wrong.
$ aws2 sagemaker-runtime invoke-endpoint \
$ --endpoint-name=pytorch-model \
$ --body=1,2 \
$ --content-type=text/csv \
$ --cli-binary-format=raw-in-base64-out \
$ output.json

An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model with message "tensors used as indices must be long, byte or bool tensors
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 125, in transform
    result = self._transform_fn(self._model, input_data, content_type, accept)
  File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 215, in _default_transform_fn
    prediction = self._predict_fn(data, model)
  File "/opt/ml/model/code/pytorch-model-reco.py", line 268, in predict_fn
    return torch.argsort(- final_matrix[input_data, :], dim = 1)
IndexError: tensors used as indices must be long, byte or bool tensors


title: How to replace a torch.tensor in-place with a new padded tensor?

body:
I am trying to figure out how I overwrite a torch.tensor object, located inside a dict, with a new torch.tensor that is a bit longer due to padding.
# pad the tensor
zeros = torch.zeros(55).long()
zeros[zeros == 0] = 100  # change to padding
temp_input = torch.cat([batch['input_ids'][0][0], zeros], dim=-1) # cat
temp_input.shape  # [567]

batch['input_ids'][0][0].shape  # [512]

batch['input_ids'][0][0] = temp_input
# The expanded size of the tensor (512) must match the existing size (567) at non-singleton dimension 0.  Target sizes: [512].  Tensor sizes: [567]

I am struggling to find a way to extend the values of a tensor in-place or to overwrite them if the dimensions change.
The dict is emitted from torch's DataLoader and looks like this:
{'input_ids': tensor([[[  101,  3720,  2011,  ..., 25786,  2135,   102]],

         [[  101,  1017,  2233,  ...,     0,     0,     0]],

         [[  101,  1996,  2899,  ..., 14262, 20693,   102]],

         [[  101,  2197,  2305,  ...,  2000,  1996,   102]]]),
 'attn_mask': tensor([[[1, 1, 1,  ..., 1, 1, 1]],

         [[1, 1, 1,  ..., 0, 0, 0]],

         [[1, 1, 1,  ..., 1, 1, 1]],

         [[1, 1, 1,  ..., 1, 1, 1]]]),
 'cats': tensor([[-0.6410,  0.1481, -2.1568, -0.6976],
         [-0.4725,  0.1481, -2.1568,  0.7869],
         [-0.6410, -0.9842, -2.1568, -0.6976],
         [-0.6410, -0.9842, -2.1568, -0.6976]], grad_fn=<StackBackward>),
 'target': tensor([[1],
         [0],
         [1],
         [1]]),
 'idx': tensor([1391, 4000,  293,  830])}


title: Run pytorch in pyodide?

body:
Is there any way I can run the python library pytorch in pyodide? I tried installing pytorch with micropip but it gives this error message:

Couldn't find a pure Python 3 wheel for 'pytorch'


title: PyTorch equivalent of numpy reshape function

body:
Hi I have these to functions to flatten my complex type data to feed it to NN and reconstruct NN prediction to the original form.
def flatten_input64(Input): #convert (:,4,4,2) complex matrix to (:,64) real vector
  Input1 = Input.reshape(-1, 32, order='F')
  Input_vector=np.zeros([19957,64],dtype = np.float64)
  Input_vector[:,0:32] = Input1.real
  Input_vector[:,32:64] = Input1.imag
  return Input_vector

def convert_output64(Output): #convert (:,64) real vector to (:,4,4,2) complex matrix
  Output1 = Output[:,0:32] + 1j * Output[:,32:64]
  output_matrix = Output1.reshape(-1, 4 ,4 ,2 , order = 'F')
  return output_matrix

I am writing a customized loss that required all operation to be in torch and I should rewrite my conversion functions in PyTorch. The problem is that PyTorch doesn't have 'F' order reshape. I tried to write my own version of F reorder but, it doesn't work.
Do you have any idea what is my mistake?
def convert_output64_torch(input):
   # number_of_samples = defined
   for i in range(0, number_of_samples):
     Output1 = input[i,0:32] + 1j * input[i,32:64]
     Output2 = Output1.view(-1,4,4,2).permute(3,2,1,0)
   if i == 0:
     Output3 = Output2
   else:
     Output3 = torch.cat((Output3, Output2),0)
return Output3

Update: following @a_guest comment I tried to recreate my matrix with transpose and reshape and I got this code working  same as F order reshape in numy:
def convert_output64_torch(input):
   Output1 = input[:,0:32] + 1j * input[:,32:64]
   shape = (-1 , 4 , 4 , 2)
   Output3 = torch.transpose(torch.transpose(torch.reshape(torch.transpose(Output1,0,1),shape[::-1]),1,2),0,3)
return Output3


title: Unable to Load Images to train model on Custom Datasets

body:
I have just stuck with Image Instance Segmentation for a while. I am trying to train the Yolact model for my custom data. Here is some brief information about what I have done so far

I have annotated the image using labelme annotation tool
I have converted annotation file for each (train & validation data) using labelme2coco -> train.json & test.json
I made changes in the cofig.py file as needed and expected by yolact
As I was following this repository I encountered an error Argument 'bb' has incorrect type to which I have solved with the approach stated in this closed issue

After completing the above task I am stuck here with below-stated issue.
Scaling parameters by 0.12 to account for a batch size of 1.
Per-GPU batch size is less than the recommended limit for batch norm. Disabling batch norm.
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py:165: UserWarning: 'lat_layers' was found in ScriptModule constants,  but it is a non-constant submodule. Consider removing it.
  " but it is a non-constant {}. Consider removing it.".format(name, hint))
/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py:165: UserWarning: 'pred_layers' was found in ScriptModule constants,  but it is a non-constant submodule. Consider removing it.
  " but it is a non-constant {}. Consider removing it.".format(name, hint))
/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py:165: UserWarning: 'downsample_layers' was found in ScriptModule constants,  but it is a non-constant submodule. Consider removing it.
  " but it is a non-constant {}. Consider removing it.".format(name, hint))
Initializing weights...
Begin training!

**_Traceback (most recent call last):_**
  File "train.py", line 504, in <module>
    train()
  File "train.py", line 270, in train
    for datum in data_loader:
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 363, in __next__
    data = self._next_data()
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 989, in _next_data
    return self._process_data(data)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 1014, in _process_data
    data.reraise()
  File "/usr/local/lib/python3.6/dist-packages/torch/_utils.py", line 395, in reraise
    raise self.exc_type(msg)
**AssertionError: Caught AssertionError in DataLoader worker process 0.**
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 185, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/content/yolact/data/coco.py", line 94, in __getitem__
    im, gt, masks, h, w, num_crowds = self.pull_item(index)
  File "/content/yolact/data/coco.py", line 141, in pull_item
    assert osp.exists(path), 'Image path does not exist: {}'.format(path)
**AssertionError: Image path does not exist: data/YolaDataset/train/6.JPG**

NOTE:I have moved my test & train data to yolact/data/YolactDataset ( also CWD is /yolact/ )
Here is the Log file
Yolact Leather Defect Config.log
Here is the content of config.py
yolact_leather_defect_dataset = Config({
    'name': 'Yolact Leather Defect',

    # Training images and annotations
    'train_images': './data/YolaDataset/train/',
    'train_info':   './data/train.json',

    # Validation images and annotations.
    'valid_images': './data/YolaDataset/test/',
    'valid_info':   './data/test.json',

    # Whether or not to load GT. If this is False, eval.py quantitative evaluation won't work.
    'has_gt': True,

    # A list of names for each of you classes.
    'class_names': ("FC", "LF", "OC", "PM"),

    # COCO class ids aren't sequential, so this is a bandage fix. If your ids aren't sequential,
    # provide a map from category_id -> index in class_names + 1 (the +1 is there because it's 1-indexed).
    # If not specified, this just assumes category ids start at 1 and increase sequentially.
    'label_map': {
        1:1, 2:2, 3:3, 4:4,
    }
})

yolact_leather_defect_config = coco_base_config.copy({
    'name': 'Yolact Leather Defect Config',

    # Dataset stuff
    'dataset': yolact_leather_defect_dataset,
    'num_classes': len(yolact_leather_defect_dataset.class_names) + 1,

    # Image Size
    'max_size': 550,

    # Training params
    'lr_steps': (280000, 600000, 700000, 750000),
    'max_iter': 800000,

    # Backbone Settings
    'backbone': resnet101_backbone.copy({
        'selected_layers': list(range(1, 4)),
        'use_pixel_scales': True,
        'preapply_sqrt': False,
        'use_square_anchors': True, # This is for backward compatability with a bug

        'pred_aspect_ratios': [ [[1, 1/2, 2]] ]*5,
        'pred_scales': [[24], [48], [96], [192], [384]],
    }),

    # FPN Settings
    'fpn': fpn_base.copy({
        'use_conv_downsample': True,
        'num_downsample': 2,
    }),

    # Mask Settings
    'mask_type': mask_type.lincomb,
    'mask_alpha': 6.125,
    'mask_proto_src': 0,
    'mask_proto_net': [(256, 3, {'padding': 1})] * 3 + [(None, -2, {}), (256, 3, {'padding': 1})] + [(32, 1, {})],
    'mask_proto_normalize_emulate_roi_pooling': True,

    # Other stuff
    'share_prediction_module': True,
    'extra_head_net': [(256, 3, {'padding': 1})],

    'positive_iou_threshold': 0.5,
    'negative_iou_threshold': 0.4,

    'crowd_iou_threshold': 0.7,

    'use_semantic_segmentation_loss': True,
})

File Structure
https://drive.google.com/file/d/1GDaDNBayMsADnxKmbIn5OL9eU17SI6eV/view?usp=sharing
I have tried to resolve the issue to the best can help myself. Any help would be appreciated.
Thank You!
[Originally this issue I have posted on yolact repo.]


title: DETECTRON2 - How do I extend DefaultMapper to include my preprocessing functionalities?

body:
How do I add a specific preprocessing pipeline to train/val data? I have a set of functions which are basically utilizing numpy and OpenCV in order to preprocess images in a specific manner before they are passed to the network. This is what I have so far:
from detectron2.data import detection_utils as utils
from detectron2.data import DatasetMapper, MetadataCatalog, build_detection_train_loader
import copy
def mapper(dataset_dict):
    dataset_dict = copy.deepcopy(dataset_dict)  # it will be modified by code below

    image = utils.read_image(dataset_dict["file_name"])
    image = pp.preprocess(image)

    auginput = T.AugInput(image)
    image = torch.from_numpy(np.float32(auginput.image))

    annos = [
        utils.transform_instance_annotations(annotation, [], (image.shape[0], image.shape[1]))
        for annotation in dataset_dict.pop("annotations")
    ]
    return {
       # create the format that the model expects
       "image": image,
       "width": image.shape[1],
       "height": image.shape[0],
       "instances": utils.annotations_to_instances(annos, (image.shape[0], image.shape[1]))
    }

class Trainer(DefaultTrainer):
    @classmethod
    def build_test_loader(cls, cfg, dataset_name):
        return build_detection_test_loader(cfg, dataset_name, mapper=DatasetMapper(cfg, False))

    @classmethod
    def build_train_loader(cls, cfg):
        return build_detection_train_loader(cfg, mapper=mapper)

cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"))
cfg.DATASETS.TRAIN = ("dataset_train",)
cfg.DATASETS.TEST = ()   # no metrics implemented for this dataset
cfg.DATALOADER.NUM_WORKERS = 2
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.0001
cfg.SOLVER.MAX_ITER = 300    # 300 iterations seems good enough, but you can certainly train longer
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128   # faster, and good enough for this toy dataset
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1

os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
trainer = Trainer(cfg)
trainer.resume_or_load(resume=False)
trainer.train()

Sadly, it looks like by overwriting the default mapper I got rid of Resize operation. My images are really large and cannot fit to GPU (i.e. 4000 x 5000).
RuntimeError: CUDA out of memory. Tried to allocate 1.93 GiB (GPU 0; 14.73 GiB total capacity; 9.81 GiB already allocated; 1.73 GiB free; 11.94 GiB reserved in total by PyTorch)
I've tried resizing them, but it looks like I am doing something wrong with bounding boxes because my regression loss is 0.00005 or something. Here is the code where I resize images:
from detectron2.data import detection_utils as utils
from detectron2.data import DatasetMapper, MetadataCatalog, build_detection_train_loader
import copy
def mapper(dataset_dict):
    dataset_dict = copy.deepcopy(dataset_dict)  # it will be modified by code below

    image = utils.read_image(dataset_dict["file_name"])
    image = pp.preprocess(image)

    auginput = T.AugInput(image)
    transform = T.Resize((1200, 800))(auginput)
    image = torch.from_numpy(np.float32(auginput.image))

    annos = [
        utils.transform_instance_annotations(annotation, [transform], (image.shape[0], image.shape[1]))
        for annotation in dataset_dict.pop("annotations")
    ]
    return {
       # create the format that the model expects
       "image": image,
       "width": image.shape[1],
       "height": image.shape[0],
       "instances": utils.annotations_to_instances(annos, (image.shape[0], image.shape[1]))
    }

class Trainer(DefaultTrainer):
    @classmethod
    def build_test_loader(cls, cfg, dataset_name):
        return build_detection_test_loader(cfg, dataset_name, mapper=DatasetMapper(cfg, False))

    @classmethod
    def build_train_loader(cls, cfg):
        return build_detection_train_loader(cfg, mapper=mapper)

Problem is that at some point I get this error:
FloatingPointError: Loss became infinite or NaN at iteration=4!
loss_dict = {'loss_cls': 0.845902681350708, 'loss_box_reg': 0.0005340162897482514, 'loss_rpn_cls': 0.5138754844665527, 'loss_rpn_loc': inf}

What do I do? How do I properly apply my preprocessing pipeline (shown in snippet below) and resize images and boudning boxes? Preprocessing pipeline class:

class PreprocessingPipeline:
  def __init__(self):
    self.clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))

  def preprocess(self, image):
    try:
      image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    except Exception as err:
      pass
    result = self.remove_label_noise(image)
    result = self.clahe.apply(result)
    result = ~result
    result = result / 255.0
    return result

  def remove_label_noise(self, image):
    thresh1 = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY)[1]
    thresh_inverted1 = 255 - thresh1

    count_cols = np.count_nonzero(thresh_inverted1, axis=0)
    first_x = np.where(count_cols>0)[0][0]
    last_x = np.where(count_cols>0)[0][-1]

    count_rows = np.count_nonzero(thresh_inverted1, axis=1)

    first_y = np.where(count_rows>0)[0][0]
    last_y = np.where(count_rows>0)[0][-1]

    crop = image[first_y:last_y-60+1, first_x:last_x+1]
    thresh2 = thresh1[first_y:last_y-60+1, first_x:last_x+1]
    thresh_inverted2 = 255 - thresh2

    contours = cv2.findContours(thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = contours[0] if len(contours) == 2 else contours[1]
    big_contour = max(contours, key=cv2.contourArea)

    mask = np.zeros_like(thresh2 , dtype=np.uint8)
    _ = cv2.drawContours(mask, [big_contour], 0, 255, -1)

    result = crop.copy()
    result[mask==0] = (0)

    return result

*** Also, I am not sure if my mapper is returning right format. I am using training FASTER RCNN for DETECTION. It says here: https://detectron2.readthedocs.io/tutorials/models.html#model-input-format I should return proposals as well, but not sure how. Please help. Thank you in advance! ***


title: DQN with policy and target networks doesn't learn properly on LunarLander enviroment

body:
I'm trying to get a hang of reinforcement learning, so I'm following a guide at:
pytorch.org/tutorials/
They've implemented DQN that solves CartPole with computer vision. Basically, I've copied their code and modified it to solve the LunarLander environment without computer vision. But I'm getting weird results. The model seems to be learning as it improves its score (with a lot of hiccups) until it fails spectacularly and gets stuck, doing weird movements and not learning.
Learning progress graph
Another learning progress graph of the different model
You can see both models failing in the same way at the end of the learning.
I cannot figure out why this solution is not working. Could you have a look at my code and perhaps find and point out errors?
Global variables:
BATCH_SIZE = 1000
GAMMA = 0.999
EPS_START = 0.9
EPS_END = 0.05
EPS_DECAY = 1000
TARGET_UPDATE = 10
LEARNING_RATE = 0.01
MOMENTUM = 0.9
MEMORY_SIZE = 10000
env = gym.make('LunarLander-v2')
n_actions = env.action_space.n
n_observation_space = env.observation_space.shape[0]
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
policy_net = DQN(n_observation_space, n_actions).to(device)
target_net = DQN(n_observation_space, n_actions).to(device)
target_net.load_state_dict(policy_net.state_dict())
target_net.eval()
optimizer = optim.Adam(policy_net.parameters(), lr=LEARNING_RATE)
memory = ReplayMemory(MEMORY_SIZE)

Learning loop:
def learn(num_episodes=50, render=False):
for i_episode in range(num_episodes):
    # Initialize the environment and state
    state = torch.tensor([env.reset()], device=device, dtype=torch.float32)
    episode_reward = 0
    for t in count():
        # Select and perform an action
        action = select_action(state)
        next_state, reward, done, _ = env.step(action.item())
        episode_reward += reward
        reward = torch.tensor([reward], device=device, dtype=torch.float32)
        next_state = torch.tensor([next_state], device=device, dtype=torch.float32)

        # Store the transition in memory
        memory.push(state, action, next_state, reward)

        # Move to the next state
        state = next_state

        # Perform one step of the optimization (on the target network)
        optimize_model()

        if render:
            env.render()

        if done:
            break
    all_rewards.append(episode_reward)
    # Update the target network, copying all weights and biases in DQN
    if i_episode % TARGET_UPDATE == 0:
        target_net.load_state_dict(policy_net.state_dict())

Optimization methods:
def optimize_model():
if len(memory) < BATCH_SIZE:
    return
transitions = memory.sample(BATCH_SIZE)
batch = Transition(*zip(*transitions))

non_final_mask = torch.tensor(tuple(map(lambda s: s is not None,
                                        batch.next_state)), device=device, dtype=torch.bool)
non_final_next_states = torch.cat([s for s in batch.next_state
                                   if s is not None])
state_batch = torch.cat(batch.state)
action_batch = torch.cat(batch.action)
reward_batch = torch.cat(batch.reward)

state_action_values = policy_net(state_batch).gather(1, action_batch)

next_state_values = torch.zeros(BATCH_SIZE, device=device)
next_state_values[non_final_mask] = target_net(non_final_next_states).max(1)[0].detach()

expected_state_action_values = (next_state_values * GAMMA) + reward_batch

loss = nn.MSELoss(state_action_values, expected_state_action_values.unsqueeze(1))

# Optimize the model
optimizer.zero_grad()
loss.backward()
for param in policy_net.parameters():
    param.grad.data.clamp_(-1, 1)
optimizer.step()

Model:
class DQN(nn.Module):

def __init__(self, input_size, output_size):
    super(DQN, self).__init__()
    self.l1 = nn.Linear(input_size, 512)
    self.l2 = nn.Linear(512, 512)
    self.l3 = nn.Linear(512, 256)
    self.l4 = nn.Linear(256, output_size)

def forward(self, x):
    x = F.leaky_relu(self.l1(x))
    x = F.leaky_relu(self.l2(x))
    x = F.leaky_relu(self.l3(x))
    return self.l4(x)

If anyone's willing to run my code locally, please let me know. I'll clean up the code and share it via Github.