# Found dtype Double but expected Float

**URL:** <https://discuss.pytorch.org/t/found-dtype-double-but-expected-float/149389>\
**Category:** Uncategorized\
**Created:** [April 17, 2022, 7:57pm UTC](https://discuss.pytorch.org/t/found-dtype-double-but-expected-float/149389 "2022-04-17T19:57:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_Br](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vincent_br/32/48744_2.png) [@Vincent\_Br](https://discuss.pytorch.org/u/Vincent_Br)\
**Post date:** [April 17, 2022, 7:57pm UTC](https://discuss.pytorch.org/t/found-dtype-double-but-expected-float/149389/1 "2022-04-17T19:57:43Z")

</div>

#!/usr/bin/env python

# coding: utf-8

from pandas import read\_csv  
import numpy as np  
import matplotlib.pyplot as plt  
#from outils import GalaxyDataset  
import os

import cv2  
from tqdm import tqdm  
from time import sleep

import random

import torch  
from torch import nn  
from torch.utils.data import DataLoader  
from torch.utils.data import random\_split  
from torchvision import datasets  
from torchvision.transforms import ToTensor  
from torch.utils.data import Dataset

import sys  
sys.path.append(“/Téléchargements/outils”)  
from outils import CLASSES  
from outils import DESCRIPTIONS  
print(CLASSES)

# In[5]:

class GalaxyDataset(Dataset):  
def **init** (  
self,  
labels,  
img\_dir,  
inds=None,  
ext=“.png”,  
transform=None,  
target\_transform=None,  
):  
“”"  
Ensemble de données pour les images de galaxies

```
    :param labels: Fichier avec les images d'entré
    :type labels: str
    :param img_dir: Dossier avec les images
    :type img_dir: str
    :param inds: indices à utiliser pour cet ensemble, defaults to None
    :type inds: List[int], optional
    :param ext: Extension des fichiers images dans img_dir, avec le point, defaults to ".png"
    :type ext: str, optional
    :param transform: Transformation PyTorch pour les images, peuvent être combinnées avec Compose au besoin, defaults to None
    :type transform: Transformation PyTorch pour les classes (probabilités), optional
    :param target_transform: Transofmation , defaults to None
    :type target_transform: utils, optional
    """
    
    self.img_labels = read_csv(labels, index_col=0) # read file skipping the first column
    self.img_labels.index = self.img_labels.index.astype(str) # index GalaxyID as str

    if inds is not None:
        # reset the index, and GalaxYID is added as a column
        #and select the rows of the train_dataset
        self.img_labels = self.img_labels.reset_index().loc[inds]             
    
    self.img_dir = img_dir
    self.ext = ext
    self.transform = transform
    self.target_transform = target_transform

def __len__ (self):
    # Donne la longueur des données
    return len(self.img_labels)

def __getitem__ (self, idx):
    # Retourne un tuple avec l'image, les probabilités et l'ID de l'image
    img_id = self.img_labels.iloc[idx,:][0] # select the GalaxyID
    img_path = os.path.join(self.img_dir, img_id + self.ext) # indcate the path to read the image
    img = cv2.imread(img_path) # read the image
    label = torch.tensor(self.img_labels.iloc[idx,:][1:].tolist()) # transform the probab to tansor

    if self.transform:
        img = self.transform(img)
    if self.target_transform:
        label = self.target_transform(label)

    return img, label, img_id

```

df=read\_csv(“/home/vincent/Documents/doc.csv”,index\_col=0)

data=np.genfromtxt(“/home/vincent/Documents/doc.csv”,delimiter=‘,’,skip\_header=1)

# ID de la galaxie

GalaxyID = data[:,0]

df\_index\_str=

for i in range (len(df.index)):  
df\_index\_str.append(str(df.index[i]))

print(df)

“”“”  
inds\_str=  
for i in range(len(inds)):  
inds\_str.append(str(inds[i]))

max\_prob=  
index\_max=  
for i in range(len(CLASSES)):  
a=CLASSES[i]  
max\_prob.append(max(df[a]))  
index\_max.append(np.argmax(df[a]))  
index\_galaxy=  
for i in range(len(CLASSES)):  
index\_galaxy.append(df.index[index\_max[i]])

def img\_is\_color(img):

```
if len(img.shape) == 3:
    # Check the color channels to see if they're all the same.
    c1, c2, c3 = img[:, : , 0], img[:, :, 1], img[:, :, 2]
    if (c1 == c2).all() and (c2 == c3).all():
        return True

return False

```

def show\_image\_list(list\_images, list\_titles=None, list\_cmaps=None, grid=True, num\_cols=2, figsize=(20, 10), title\_fontsize=30):  
‘’’  
Shows a grid of images, where each image is a Numpy array. The images can be either  
RGB or grayscale.

```
Parameters:
----------
images: list
    List of the images to be displayed.
list_titles: list or None
    Optional list of titles to be shown for each image.
list_cmaps: list or None
    Optional list of cmap values for each image. If None, then cmap will be
    automatically inferred.
grid: boolean
    If True, show a grid over each image
num_cols: int
    Number of columns to show.
figsize: tuple of width, height
    Value to be passed to pyplot.figure()
title_fontsize: int
    Value to be passed to set_title().
'''

assert isinstance(list_images, list)
assert len(list_images) > 0
assert isinstance(list_images[0], np.ndarray)

if list_titles is not None:
    assert isinstance(list_titles, list)
    assert len(list_images) == len(list_titles), '%d imgs != %d titles' % (len(list_images), len(list_titles))

if list_cmaps is not None:
    assert isinstance(list_cmaps, list)
    assert len(list_images) == len(list_cmaps), '%d imgs != %d cmaps' % (len(list_images), len(list_cmaps))

num_images = len(list_images)
num_cols = min(num_images, num_cols)
num_rows = int(num_images / num_cols) + (1 if num_images % num_cols != 0 else 0)

# Create a grid of subplots.
fig, axes = plt.subplots(num_rows, num_cols, figsize=figsize)

# Create list of axes for easy iteration.
if isinstance(axes, np.ndarray):
    list_axes = list(axes.flat)
else:
    list_axes = [axes]

for i in range(num_images):

    img = list_images[i]
    title = list_titles[i] if list_titles is not None else 'Image %d' % (i)
    cmap = list_cmaps[i] if list_cmaps is not None else (None if img_is_color(img) else 'gray')
    
    list_axes[i].imshow(img, cmap=cmap)
    list_axes[i].set_title(title, fontsize=title_fontsize) 
    list_axes[i].grid(grid)

for i in range(num_images, len(list_axes)):
    list_axes[i].set_visible(False)

fig.tight_layout()
_ = plt.show()

```

# In[21]:

list\_images=  
list\_title=DESCRIPTIONS  
print(list\_title)  
for i in range(len(CLASSES)):  
index\_instr=str(index\_galaxy[i])  
path=r"/home/vincent/Documents/images\_training\_rev1/“+index\_instr+”.jpg" #a voir  
img = cv2.imread(path)  
list\_images.append(img)

show\_image\_list(list\_images,list\_title, num\_cols=4,title\_fontsize=10,figsize=(20, 20))

####### fin Etape1

# Etape 2

# ATTENTION NE PAS EXECUTUER CETTE CELLULE

# In:

“”"  
#reduire la dim des images et les sauvgarder dans un niuveau dossier.

######for i in tqdm(range(len(df.index))): #C++’est quoi tqdm ?  
for i in range(len(df.index)):

# #specifier le Chemin ou lire l’image

```
path=r"/home/vincent/Documents/images_training_rev1/"+str(df.index[i])+".jpg" 

```

# #read the image 424x424

```
img = cv2.imread(path, 1) 

```

# Extracting the Region of Interest (ROI): (212x212) à partir du centre

```
half = img[106:318, 106:318]

```

#resize the ROI 64x64 pixels  
img\_64 = cv2.resize(half, (64, 64))

#saving the output of cv2.resize in new file as png image.  
cv2.imwrite(r"/home/vincent/Documents/Image64/“+str(df.index[i])+”.png",img\_64)

```
sleep(0.02) #obligé sleep ???

```

“”"

# In[22]:

dataset\_size = len(GalaxyID) # taille de data  
test\_size = int(0.2\* dataset\_size) # On veut que 20 % des images servent aux tests.  
train\_size = dataset\_size - test\_size # taille de datat disponible pour l’entrainement

# On split aleatoirement les data en 2 deux sous-ensembles

train\_dataset, test\_dataset = random\_split(GalaxyID,  
[train\_size, test\_size])

# indices des deux ensembles dans GalaxyID

train\_inds = list(np.where(train\_dataset[:][i]==GalaxyID)[0][0] for i in  
tqdm(range(len(train\_dataset[:]))))

test\_inds = list(np.where(test\_dataset[:][i]==GalaxyID)[0][0] for i in  
tqdm(range(len(test\_dataset[:]))))

# In[215]:

# call GalaxyDataset to return the image, probab and GalaxyID

train\_model = GalaxyDataset(“/home/vincent/Documents/doc.csv”,r"/home/vincent/Documents/Image64/",train\_inds,transform=ToTensor())

test\_model = GalaxyDataset(“/home/vincent/Documents/doc.csv”,r"/home/vincent/Documents/Image64/",test\_inds,transform=ToTensor())

“”" DataLoader gives us batches of 64 images from trainsets,  
randomizing their order (shuffle=True) “”"  
train\_loader = DataLoader(train\_model, batch\_size = 64, shuffle=True)  
test\_loader = DataLoader(test\_model, batch\_size = 64, shuffle=True)

# In[37]:

list\_images=  
list\_title=DESCRIPTIONS  
print(list\_title)  
for i in range(len(CLASSES)):  
index\_instr=str(index\_galaxy[i])  
path=r"/home/vincent/Documents/Image64/“+index\_instr+”.png" #a voir  
img = cv2.imread(path)  
list\_images.append(img)

show\_image\_list(list\_images,list\_title, num\_cols=4,title\_fontsize=10,figsize=(20, 20))

####### fin Etape2

# ETAPE 3

from torch import nn  
import utils as ut

def get\_output\_width(input\_width, kernel\_size, stride=1, padding=0, dilation=1):  
# Fonction pour calculer la largeur de sortie d’une couche de convolution  
# Ref: [Conv2d — PyTorch 2.9 documentation](https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d)  
# Attention: stride=kernel\_size pour max pooling par défaut

```
return int(
    (input_width + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1
)

```

device = “cuda” if torch.cuda.is\_available() else “cpu”

class ConvNet(nn.Module):

```
def __init__ (self):
    
    super(ConvNet, self). __init__ ()
    # NOTE: On pourrait avoir une seule séquence. J'ai séparé pour la clarté

    # Pooling 2x2, assigné à pool car on le réutilise plusieurs fois
    self.pool = nn.MaxPool2d(2)

    # Séquence de convolutions
    self.conv_stack = nn.Sequential(
        #nn.Conv2d(1, 6, 5,padding=3),
        nn.Conv2d(in_channels=3, out_channels=6, kernel_size=5,padding=3),
        # batch normalization normalise les poids dans une couche
        nn.BatchNorm2d(6),
        nn.ReLU(),
        self.pool,
        nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5,padding=3),
        nn.BatchNorm2d(16),
        nn.ReLU(),
        self.pool,
    )

    # Opérations pour convertir les images 2d en 1d
    self.flatten = nn.Flatten(start_dim=1)
    # stride=kernel_size pour maxpool par défaut
    self.linear_entry_dim =2
    """
    self.linear_entry_dim = get_output_width(get_output_width(
        get_output_width(get_output_width(64, 5,padding=3), 5, stride=2,padding=3), 5
    ), 5, stride=2,padding=3)
    """
    
    # Séquence de couches pleinement connectées
    self.linear_stack = nn.Sequential(
        # Dropout de 20% des unités dans la prochaine couche
        # nn.Dropout(p=0.2),
        #nn.Linear(16 * self.linear_entry_dim**2, 120),
        nn.Linear(4624 , 120),
        nn.ReLU(),
        nn.Linear(120, 84),
        nn.ReLU(),
        nn.Linear(84, 37),
    )

def forward(self, x):
    print(x.shape)
    # La séquence de convolution d'abbord
    x = self.conv_stack(x)
    print(x.shape)
    # On applatit l'image
    x = self.flatten(x)
    # La séquence pleinement connectée
    logits = self.linear_stack(x)
    return logits

```

#Utilisez une fonction sigmoide pour transforméé les sorties linéaires (logits) en valeurs entre 0 et 1. ?

model = ConvNet().to(device)

labels\_map = {  
0: “Smooth”,  
1: “Featured or disc”,  
2: “Star or artifact”,  
3: “Edge on”,  
4: “Not edge on”,  
5: “Bar through center”,  
6: “No bar”,  
7: “Spiral”,  
8: “No Spiral”,  
9: “No bulge”,  
10: “No bulge”,  
11: “Just noticeable bulge”,  
12: “Obvious bulge”,  
13: “Dominant bulge”,  
14: “Odd Feature”,  
15: “No Odd Feature”,  
16: “Completely round”,  
17: “In between”,  
18: “Cigar shaped”,  
19: “Ring”,  
20: “Lens or arc”,  
21: “Disturbed”,  
22: “Irregular”,  
23: “Other”,  
24: “Merger”,  
25: “Dust lane”,  
26: “Rounded bulge”,  
27: “Boxy bulge”,  
28: “No bulge”,  
29: “Tightly wound arms”,  
30: “Medium wound arms”,  
31: “Loose wound arms”,  
32: “1 Spiral Arm”,  
33: “2 Spiral Arms”,  
34: “3 Spiral Arms”,  
35: “4 Spiral Arms”,  
36: “More than four Spiral Arms”,  
37: “Can’t tell how many spiral arms”,

}

inv\_labels\_map = dict(zip(labels\_map.values(), labels\_map.keys()))

learning\_rate = 1e-3  
batch\_size = 64  
epochs = 6

loss\_fn = nn.MSELoss()

#Adam est un optimiseur plus flexible que SGD  
optimizer = torch.optim.Adam(model.parameters())

def train\_loop(dataloader, model, loss\_fn, optimizer):

```
# Taille de l'ensemble d'entraînement
size = len(dataloader.dataset)

# Itération sur les batches (dataloader nous donne les données par batch)
# X est l'image et y la classe
train_loss = 0.0
for batch,(X,y,z) in enumerate(dataloader):
    # Fonction objectif
    pred = model(X) # prédiction
    loss = loss_fn(pred, y)

    # Rétropropagation
    optimizer.zero_grad() # On réinitialise le gradient d'abord
    loss.backward() # Rétropropagation
    optimizer.step() # On fait un pas dans l'espace paramètre

    loss, current = loss.item(), batch * len(X)
    train_loss += loss * X.size(0)
    # Progrès
    if batch % 100 == 0:
        print(f"Loss: {loss}, [{current}/{size}]")

return train_loss / size

```

def test\_loop(dataloader, model, loss\_fn):  
size = len(dataloader.dataset)  
num\_batches = len(dataloader)  
test\_loss = correct = 0

```
# On se sauve les gradients comme ils ne sont pas utilisés
with torch.no_grad():
    for X, y in dataloader:
        pred = model(X)
        test_loss += loss_fn(pred, y).item() # Compute loss
        correct += (pred.argmax(1) == y).type(torch.float).sum().item()

test_loss /= num_batches
correct /= size
print(f"Test Error: \n Accuracy: {(100*correct)}%, Avg loss: {test_loss} \n")

return test_loss

```

#HERE MY PROBLEM

epochs = 6  
train\_losses =   
test\_losses =   
for t in range(epochs):  
print(f"Epoch {t+1}\n-------------------------------")  
train\_loss = train\_loop(train\_loader, model, loss\_fn, optimizer)  
train\_losses.append(train\_loss)  
test\_loss = test\_loop(test\_loader, model, loss\_fn)  
test\_losses.append(test\_loss)  
print(“Done!”)

* * *

Hi, im trying to create a network with my pytorch but Im stuck on this error for more than 6h now  
“Found dtype Double but expected Float” that occur when I call my training loop. Can someone help me pls ?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 18, 2022, 6:38am UTC](https://discuss.pytorch.org/t/found-dtype-double-but-expected-float/149389/2 "2022-04-18T06:38:49Z")

</div>

Your code is hard to read as you didn’t format it. The error message is raised e.g. if your input data and the model parameters show a `dtype` mismatch.  
I would guess that your data might be a `DoubleTensor`, so call `x = x.float()` on the input tensor before passing it to the model.
