transforms.ColorJitter. TypeError: '>' not supported between instances of 'tuple' and 'int'

username: nickhir
cooked: <p>Hello,<br>
when I try to transform my image using ColorJitter I get the following error:<br>
TypeError: ‘&gt;’ not supported between instances of ‘tuple’ and ‘int’.<br>
My transforms operation looks something like this:</p>
<p>transformations=transforms.Compose([<br>
transforms.ColorJitter(brightness=0.2,saturation=(0.8,1.62),contrast=(1,1.3),hue=(-0.02,0.02)),<br>
transforms.RandomAffine(degrees=180,translate=(0.05,0.05),scale=(0.9,1.1),shear=25),<br>
transforms.CenterCrop((64,64)),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])<br>
])</p>
<p>I am confused because I thought a tuple as an input was allowed.<br>
Hopefully someone can help me</p>

username: ptrblck
cooked: <p>Could you post the code which throws this error?<br>
Are you trying to pass a <code>tuple</code> of images to this transformation?</p>

username: nickhir
cooked: <pre><code class="lang-auto">import pandas as pd
from Modules import *
import torch.nn as nn
import re
import torch.nn.functional as F
import torch
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import Dataset,DataLoader
import openslide
import numpy as np
from PIL import Image
from torchvision import transforms
import torch.multiprocessing
import time
import random
from torch.autograd import grad_mode
import matplotlib.pyplot as plt

start_time=time.time()

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


class LoadDataset(Dataset):
    def __init__(self,csv_file,slide_path,img_size=150,transform=None):

        self.csv=pd.read_csv(csv_file,header=None)
        self.transform=transform
        self.slide_path=slide_path
        self.img_size=img_size
        self.pre_file=re.search(r"(.*)(.xml)",self.csv.iloc[0,0]).group(1)
        self.Slide=openslide.open_slide(self.slide_path + self.pre_file+ ".ndpi")

    def __len__(self):
        return len(self.csv)

    def __getitem__(self,idx):
        filename=re.search(r"(.*)(.xml)",self.csv.iloc[idx,0]).group(1)
        X_Coordinate=self.csv.iloc[idx,1]
        Y_Coordinate=self.csv.iloc[idx,2]
        label=torch.Tensor((1,0)) if self.csv.iloc[idx,3] == 1 else torch.Tensor((0,1))
        if self.pre_file != filename:
            self.Slide=openslide.open_slide(self.slide_path + filename + ".ndpi")

        img=self.Slide.read_region((X_Coordinate - int(self.img_size / 2),Y_Coordinate - int(self.img_size / 2)),
                              0, (self.img_size,self.img_size)).convert("RGB")
        self.pre_file=filename

        if self.transform:
            trans_img=self.transform(img)
        else:
            trans_img=transforms.ToTensor()(img)

        return (trans_img,label)


class LoadValset(Dataset):
    # noch implementieren
    pass


if __name__ == '__main__':
    transformations=transforms.Compose([
        transforms.ColorJitter(brightness=(0.8,1.1),saturation=(0.8,1.62),contrast=(1,1.3),hue=(-0.02,0.02)),
        transforms.RandomAffine(degrees=180,translate=(0.05,0.05),scale=(0.9,1.1),shear=25),
        transforms.CenterCrop((64,64)),
        transforms.ToTensor(),
        transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
    ])

    Trainset=LoadDataset(csv_file=r"E:\Output_csv\Output.csv", slide_path=r"E:\Image_files\\",img_size=150,
                         transform=transformations)
    batch_size=64
    TrainLoader=DataLoader(Trainset, batch_size=batch_size, shuffle=True)


    model=simple_CNN()
    model.to(device)

    optimizer=optim.Adam(model.parameters(),lr=0.001)
    criterion=F.binary_cross_entropy
    scheduler=StepLR(optimizer,50)


    def train(epoch):
        model.train()
        running_loss=0.0
        for batch_id,(data,label) in enumerate(TrainLoader):
            data,label=data.to(device),label.to(device)
            optimizer.zero_grad()
            out=model(data)
            loss=criterion(out,label)
            running_loss+=loss
            loss.backward()
            optimizer.step()
            scheduler.step()

        print("Training Epoch %d: running loss: %f" % (epoch,running_loss * batch_size / len(Trainset)))

    for epoch in range(1,20):
        train(epoch)

    print((time.time() - start_time) / 60)

</code></pre>
<p>Here is the code. I dont think i pass a tuple of images.<br>
If I just delete the ColorJitter line the code works.<br>
I know my code is a mess, I have just started programming ^^.<br>
Any help is appreciated!</p>

username: ptrblck
cooked: <p>That’s strange, as your code seems to work for this dummy input image:</p>
<pre><code class="lang-python">import torchvision.transforms.functional as TF

img = TF.to_pil_image(torch.randn(3, 224, 224))

transformations=transforms.Compose([
    transforms.ColorJitter(brightness=0.2,saturation=(0.8,1.62),contrast=(1,1.3),hue=(-0.02,0.02)),
    transforms.RandomAffine(degrees=180,translate=(0.05,0.05),scale=(0.9,1.1),shear=25),
    transforms.CenterCrop((64,64)),
    transforms.ToTensor(),
    transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
])

x = transformations(img)
</code></pre>
<p>Could you check the shape and type of <code>img</code> before passing it to <code>self.transform</code>?</p>

username: nickhir
cooked: <p>Thanks for the quick reply</p>
<pre><code class="lang-auto">&lt;PIL.Image.Image image mode=RGB size=150x150 at 0x2B64A9BA5C18&gt;
(150, 150)
&lt;class 'PIL.Image.Image'&gt;
Traceback (most recent call last):
  File "DataLoader_outline.py", line 122, in &lt;module&gt;
    train(epoch)
  File "DataLoader_outline.py", line 96, in train
    for batch_id,(data,label) in enumerate(TrainLoader):
  File "/home/hd/hd_hd/hd_qo450/miniconda3/envs/bachelorarbeit/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 264, in __next__
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/home/hd/hd_hd/hd_qo450/miniconda3/envs/bachelorarbeit/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 264, in &lt;listcomp&gt;
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "DataLoader_outline.py", line 53, in __getitem__
    trans_img=self.transform(img)
  File "/home/hd/hd_hd/hd_qo450/miniconda3/envs/bachelorarbeit/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 49, in __call__
    img = t(img)
  File "/home/hd/hd_hd/hd_qo450/miniconda3/envs/bachelorarbeit/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 755, in __call__
    self.saturation, self.hue)
  File "/home/hd/hd_hd/hd_qo450/miniconda3/envs/bachelorarbeit/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 725, in get_params
    if brightness &gt; 0:
TypeError: '&gt;' not supported between instances of 'tuple' and 'int'
</code></pre>
<p>Thats the whole Error.</p>
<p>I installt pytorch / torchvision via</p>
<pre><code class="lang-auto">conda install pytorch torchvision cudatoolkit=9.0 -c pytorch

</code></pre>

username: ptrblck
cooked: <p>This line of code is outdated. Anyway, since <code>brightness</code> is a single float value, I’m not sure why it throws an error.<br>
Could you check your <code>torchvision.__version__</code> so that we can have a look?</p>

username: nickhir
cooked: <aside class="quote no-group quote-modified" data-username="nickhir" data-post="5" data-topic="46517">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/46a35a/48.png" class="avatar"> nickhir:</div>
<blockquote>
<p>File “/home/hd/hd_hd/hd_qo450/miniconda3/envs/bachelorarbeit/lib/python3.6/site-packages/torchvision/transforms/transforms.py”, line 725, in get_params if brightness &gt; 0: TypeError: ‘&gt;’ not supported between instances of ‘tuple’ and ‘int’</p>
</blockquote>
</aside>
<p>This error was generated while i tried brightness=(0.8,1.1), sorry for the confusion.<br>
Im using version 0.2.1</p>

username: ptrblck
cooked: <p>Thanks for the information.<br>
The <code>tuple</code> argument is used in newer <code>torchvision</code> versions, so you might update this lib.</p>

username: nickhir
cooked: <p>Thank you very much!!! Works like a charm.<br>
I thought id have the latest version because i just downloaded it 3 day ago.</p>