Found dtype Double but expected Float

Vincent_Br: <p>#!/usr/bin/env python</p>
<h1>
<a name="coding-utf-8-1" class="anchor" href="#coding-utf-8-1"></a>coding: utf-8</h1>
<p>from pandas import read_csv<br>
import numpy as np<br>
import matplotlib.pyplot as plt<br>
<span class="hashtag">#from</span> outils import GalaxyDataset<br>
import os</p>
<p>import cv2<br>
from tqdm import tqdm<br>
from time import sleep</p>
<p>import random</p>
<p>import torch<br>
from torch import nn<br>
from torch.utils.data import DataLoader<br>
from torch.utils.data import random_split<br>
from torchvision import datasets<br>
from torchvision.transforms import ToTensor<br>
from torch.utils.data import Dataset</p>
<p>import sys<br>
sys.path.append("/Téléchargements/outils")<br>
from outils import CLASSES<br>
from outils import DESCRIPTIONS<br>
print(CLASSES)</p>
<h1>
<a name="in5-2" class="anchor" href="#in5-2"></a>In[5]:</h1>
<p>class GalaxyDataset(Dataset):<br>
def <strong>init</strong>(<br>
self,<br>
labels,<br>
img_dir,<br>
inds=None,<br>
ext=".png",<br>
transform=None,<br>
target_transform=None,<br>
):<br>
“”"<br>
Ensemble de données pour les images de galaxies</p>
<pre><code>    :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
</code></pre>
<p>df=read_csv("/home/vincent/Documents/doc.csv",index_col=0)</p>
<p>data=np.genfromtxt("/home/vincent/Documents/doc.csv",delimiter=’,’,skip_header=1)</p>
<h1>
<a name="id-de-la-galaxie-3" class="anchor" href="#id-de-la-galaxie-3"></a>ID de la galaxie</h1>
<p>GalaxyID = data[:,0]</p>
<p>df_index_str=[]</p>
<p>for i in range (len(df.index)):<br>
df_index_str.append(str(df.index[i]))</p>
<p>print(df)</p>
<p>“”""<br>
inds_str=[]<br>
for i in range(len(inds)):<br>
inds_str.append(str(inds[i]))</p>
<p>max_prob=[]<br>
index_max=[]<br>
for i in range(len(CLASSES)):<br>
a=CLASSES[i]<br>
max_prob.append(max(df[a]))<br>
index_max.append(np.argmax(df[a]))<br>
index_galaxy=[]<br>
for i in range(len(CLASSES)):<br>
index_galaxy.append(df.index[index_max[i]])</p>
<p>def img_is_color(img):</p>
<pre><code>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
</code></pre>
<p>def show_image_list(list_images, list_titles=None, list_cmaps=None, grid=True, num_cols=2, figsize=(20, 10), title_fontsize=30):<br>
‘’’<br>
Shows a grid of images, where each image is a Numpy array. The images can be either<br>
RGB or grayscale.</p>
<pre><code>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) &gt; 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()
</code></pre>
<h1>
<a name="in21-4" class="anchor" href="#in21-4"></a>In[21]:</h1>
<p>list_images=[]<br>
list_title=DESCRIPTIONS<br>
print(list_title)<br>
for i in range(len(CLASSES)):<br>
index_instr=str(index_galaxy[i])<br>
path=r"/home/vincent/Documents/images_training_rev1/"+index_instr+".jpg" <span class="hashtag">#a</span> voir<br>
img = cv2.imread(path)<br>
list_images.append(img)</p>
<p>show_image_list(list_images,list_title, num_cols=4,title_fontsize=10,figsize=(20, 20))</p>
<p>####### fin Etape1</p>
<h1>
<a name="etape-2-5" class="anchor" href="#etape-2-5"></a>Etape 2</h1>
<h1>
<a name="attention-ne-pas-executuer-cette-cellule-6" class="anchor" href="#attention-ne-pas-executuer-cette-cellule-6"></a>ATTENTION NE PAS EXECUTUER CETTE CELLULE</h1>
<h1>
<a name="in-7" class="anchor" href="#in-7"></a>In[ ]:</h1>
<p>“”"<br>
<span class="hashtag">#reduire</span> la dim des images et les sauvgarder dans un niuveau dossier.</p>
<p>#####<span class="hashtag">#for</span> i in tqdm(range(len(df.index))):  <span class="hashtag">#C</span>’est quoi tqdm ?<br>
for i in range(len(df.index)):</p>
<h1>
<a name="specifier-le-chemin-ou-lire-limage-8" class="anchor" href="#specifier-le-chemin-ou-lire-limage-8"></a><span class="hashtag">#specifier</span> le Chemin ou lire l’image</h1>
<pre><code>path=r"/home/vincent/Documents/images_training_rev1/"+str(df.index[i])+".jpg" 
</code></pre>
<h1>
<a name="read-the-image-424x424-9" class="anchor" href="#read-the-image-424x424-9"></a><span class="hashtag">#read</span> the image 424x424</h1>
<pre><code>img = cv2.imread(path, 1) 
</code></pre>
<h1>
<a name="extracting-the-region-of-interest-roi-212x212-partir-du-centre-10" class="anchor" href="#extracting-the-region-of-interest-roi-212x212-partir-du-centre-10"></a>Extracting the Region of Interest (ROI): (212x212) à partir du centre</h1>
<pre><code>half = img[106:318, 106:318]
</code></pre>
<p><span class="hashtag">#resize</span> the ROI 64x64 pixels<br>
img_64 = cv2.resize(half, (64, 64))</p>
<p><span class="hashtag">#saving</span> the output of cv2.resize in new file as png image.<br>
cv2.imwrite(r"/home/vincent/Documents/Image64/"+str(df.index[i])+".png",img_64)</p>
<pre><code>sleep(0.02)      #obligé sleep ???
</code></pre>
<p>“”"</p>
<h1>
<a name="in22-11" class="anchor" href="#in22-11"></a>In[22]:</h1>
<p>dataset_size = len(GalaxyID)  # taille de data<br>
test_size = int(0.2* dataset_size)  # On veut que 20 % des images servent aux tests.<br>
train_size = dataset_size - test_size # taille de datat disponible pour l’entrainement</p>
<h1>
<a name="on-split-aleatoirement-les-data-en-2-deux-sous-ensembles-12" class="anchor" href="#on-split-aleatoirement-les-data-en-2-deux-sous-ensembles-12"></a>On split aleatoirement les data en 2 deux sous-ensembles</h1>
<p>train_dataset, test_dataset = random_split(GalaxyID,<br>
[train_size, test_size])</p>
<h1>
<a name="indices-des-deux-ensembles-dans-galaxyid-13" class="anchor" href="#indices-des-deux-ensembles-dans-galaxyid-13"></a>indices des deux ensembles dans GalaxyID</h1>
<p>train_inds = list(np.where(train_dataset[:][i]==GalaxyID)[0][0] for i in<br>
tqdm(range(len(train_dataset[:]))))</p>
<p>test_inds = list(np.where(test_dataset[:][i]==GalaxyID)[0][0] for i in<br>
tqdm(range(len(test_dataset[:]))))</p>
<h1>
<a name="in215-14" class="anchor" href="#in215-14"></a>In[215]:</h1>
<h1>
<a name="call-galaxydataset-to-return-the-image-probab-and-galaxyid-15" class="anchor" href="#call-galaxydataset-to-return-the-image-probab-and-galaxyid-15"></a>call GalaxyDataset to return the image, probab and GalaxyID</h1>
<p>train_model = GalaxyDataset("/home/vincent/Documents/doc.csv",r"/home/vincent/Documents/Image64/",train_inds,transform=ToTensor())</p>
<p>test_model = GalaxyDataset("/home/vincent/Documents/doc.csv",r"/home/vincent/Documents/Image64/",test_inds,transform=ToTensor())</p>
<p>“”" DataLoader gives us batches of 64 images from trainsets,<br>
randomizing their order (shuffle=True) “”"<br>
train_loader = DataLoader(train_model, batch_size = 64, shuffle=True)<br>
test_loader = DataLoader(test_model, batch_size = 64, shuffle=True)</p>
<h1>
<a name="in37-16" class="anchor" href="#in37-16"></a>In[37]:</h1>
<p>list_images=[]<br>
list_title=DESCRIPTIONS<br>
print(list_title)<br>
for i in range(len(CLASSES)):<br>
index_instr=str(index_galaxy[i])<br>
path=r"/home/vincent/Documents/Image64/"+index_instr+".png" <span class="hashtag">#a</span> voir<br>
img = cv2.imread(path)<br>
list_images.append(img)</p>
<p>show_image_list(list_images,list_title, num_cols=4,title_fontsize=10,figsize=(20, 20))</p>
<p>####### fin Etape2</p>
<h1>
<a name="etape-3-17" class="anchor" href="#etape-3-17"></a>ETAPE 3</h1>
<p>from torch import nn<br>
import utils as ut</p>
<p>def get_output_width(input_width, kernel_size, stride=1, padding=0, dilation=1):<br>
# Fonction pour calculer la largeur de sortie d’une couche de convolution<br>
# Ref: <a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d" class="inline-onebox" rel="noopener nofollow ugc">Conv2d — PyTorch 1.11.0 documentation</a><br>
# Attention: stride=kernel_size pour max pooling par défaut</p>
<pre><code>return int(
    (input_width + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1
)
</code></pre>
<p>device = “cuda” if torch.cuda.is_available() else “cpu”</p>
<p>class ConvNet(nn.Module):</p>
<pre><code>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
</code></pre>
<p><span class="hashtag">#Utilisez</span> une fonction sigmoide pour transforméé les sorties linéaires (logits) en valeurs entre 0 et 1. ?</p>
<p>model = ConvNet().to(device)</p>
<p>labels_map = {<br>
0: “Smooth”,<br>
1: “Featured or disc”,<br>
2: “Star or artifact”,<br>
3: “Edge on”,<br>
4: “Not edge on”,<br>
5: “Bar through center”,<br>
6: “No bar”,<br>
7: “Spiral”,<br>
8: “No Spiral”,<br>
9: “No bulge”,<br>
10: “No bulge”,<br>
11: “Just noticeable bulge”,<br>
12: “Obvious bulge”,<br>
13: “Dominant bulge”,<br>
14: “Odd Feature”,<br>
15: “No Odd Feature”,<br>
16: “Completely round”,<br>
17: “In between”,<br>
18: “Cigar shaped”,<br>
19: “Ring”,<br>
20: “Lens or arc”,<br>
21: “Disturbed”,<br>
22: “Irregular”,<br>
23: “Other”,<br>
24: “Merger”,<br>
25: “Dust lane”,<br>
26: “Rounded bulge”,<br>
27: “Boxy bulge”,<br>
28: “No bulge”,<br>
29: “Tightly wound arms”,<br>
30: “Medium wound arms”,<br>
31: “Loose wound arms”,<br>
32: “1 Spiral Arm”,<br>
33: “2 Spiral Arms”,<br>
34: “3 Spiral Arms”,<br>
35: “4 Spiral Arms”,<br>
36: “More than four Spiral Arms”,<br>
37: “Can’t tell how many spiral arms”,</p>
<p>}</p>
<p>inv_labels_map = dict(zip(labels_map.values(), labels_map.keys()))</p>
<p>learning_rate = 1e-3<br>
batch_size = 64<br>
epochs = 6</p>
<p>loss_fn = nn.MSELoss()</p>
<p><span class="hashtag">#Adam</span> est un optimiseur plus flexible que SGD<br>
optimizer = torch.optim.Adam(model.parameters())</p>
<p>def train_loop(dataloader, model, loss_fn, optimizer):</p>
<pre><code># 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
</code></pre>
<p>def test_loop(dataloader, model, loss_fn):<br>
size = len(dataloader.dataset)<br>
num_batches = len(dataloader)<br>
test_loss = correct = 0</p>
<pre><code># 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
</code></pre>
<p><span class="hashtag">#HERE</span> MY PROBLEM</p>
<p>epochs = 6<br>
train_losses = []<br>
test_losses = []<br>
for t in range(epochs):<br>
print(f"Epoch {t+1}\n-------------------------------")<br>
train_loss = train_loop(train_loader, model, loss_fn, optimizer)<br>
train_losses.append(train_loss)<br>
test_loss = test_loop(test_loader, model, loss_fn)<br>
test_losses.append(test_loss)<br>
print(“Done!”)</p>
<hr>
<p>Hi, im trying to create a network with my pytorch but Im stuck on this error for more than 6h now<br>
“Found dtype Double but expected Float” that occur when I call my training loop. Can someone help me pls ?</p>

ptrblck: <p>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 <code>dtype</code> mismatch.<br>
I would guess that your data might be a <code>DoubleTensor</code>, so call <code>x = x.float()</code> on the input tensor before passing it to the model.</p>