Problem in CONVOLUTIONAL VARIATIONAL AUTOENCODER

Parama_Bagchi: <p>Respected Sir,<br>
I AM TRYING TO CONSTRUCT CVAE WHICH TAKES A 256X256 IMAGE AND OUTPUTS THE SAME. THAT IS I WILL TAKE  RAW IMAGE AND TRAIN MY CVAE. AND THEN SEE HOW CLOSELY IT RESEMBLES AN ENHANCED IMAGE. AFTER THAT I SHALL PASS A RAW TEST IMAGE TO MY TRIANED MODEL, AND SEE HOW CLOSELY IT RESMBLES MY ENHANCED TEST IMAGE. I HAVE USED THE FOLLOWING PYTORCH CODE, FOR PREPARING THE TEST-LOADER BUT ERROR IS COMING WHILE RUNNIGN IN GOOGLE COLAB. PLEASE HELP ME TO DEBUG IT.</p>
<p>class prepare_dataset</p>
<p>all_image_pathtestraw = []<br>
for i in tqdm(range(len(image_dirs))):<br>
image_pathtestraw = glob.glob(f"/content/drive/MyDrive/Autoencoders1/test/raw/*")<br>
image_pathtestraw.sort()<br>
for image_path in image_pathtestraw:<br>
all_image_pathtestraw.append(image_path)<br>
train_data = all_image_paths[1:4000]<br>
valid_data = all_image_pathsval[1:4000]<br>
test_data = all_image_pathtestraw[1:1000]</p>
<p>batch_size = 15<br>
transform = transform()</p>
<h1>
<a name="prepare-the-training-and-validation-data-loaders-1" class="anchor" href="#prepare-the-training-and-validation-data-loaders-1"></a>prepare the training and validation data loaders</h1>
<p>train_data, valid_data, test_data = prepare_dataset(<br>
ROOT_PATH=‘/content/drive/MyDrive/Autoencoders1/’<br>
)<br>
class Dataset(Dataset):<br>
def <strong>init</strong>(self, data_list, transform):<br>
self.data = data_list<br>
self.transform = transform</p>
<pre><code>def __len__(self):
    return len(self.data)

def __getitem__(self, index):
    image = cv2.imread(self.data[index])
    image = cv2.resize(image, (256,256))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = self.transform(image)
    return image
</code></pre>
<p>trainset = Dataset(train_data, transform=transform)<br>
<span class="hashtag">#train_loader</span> = DataLoader(trainset, batch_size=batch_size, shuffle=True)<br>
validset = Dataset(valid_data, transform=transform)<br>
<span class="hashtag">#val_loader</span> = DataLoader(validset, batch_size=batch_size)<br>
testset = Dataset(test_data, transform=transform)<br>
extracting training images<br>
training_images = [x for x in trainset.data]</p>
<h1>
<a name="extracting-validation-images-2" class="anchor" href="#extracting-validation-images-2"></a>extracting validation images</h1>
<p>validation_images = [x for x in validset.data]</p>
<h1>
<a name="extracting-test-images-for-visualization-purposes-3" class="anchor" href="#extracting-test-images-for-visualization-purposes-3"></a>extracting test images for visualization purposes</h1>
<p>test_images = [x for x in testset.data]<br>
class Custom(Dataset):<br>
def <strong>init</strong>(self, data, transforms=None):<br>
self.data = data<br>
self.transforms = transforms</p>
<p>def <strong>len</strong>(self):<br>
return len(self.data)</p>
<p>def <strong>getitem</strong>(self, idx):<br>
image = self.data[idx]</p>
<pre><code>if self.transforms!=None:
  image = self.transforms(image)
return image
</code></pre>
<h1>
<a name="creating-pytorch-datasets-4" class="anchor" href="#creating-pytorch-datasets-4"></a>creating pytorch datasets</h1>
<p>training_data = Custom(training_images, transforms=transforms.Compose([transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))<br>
validation_data = Custom(validation_images, transforms=transforms.Compose([transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))<br>
test_data = Custom(test_images, transforms=transforms.Compose([transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))</p>
<h1>
<a name="training-model-5" class="anchor" href="#training-model-5"></a>training model</h1>
<p>model = ConvolutionalAutoencoder(Autoencoder(Encoder(), Decoder()))</p>
<p>log_dict = model.train(nn.MSELoss() , epochs=1, batch_size=15,<br>
training_set=training_data, validation_set=validation_data,<br>
testset=test_data)<br>
TypeError                                 Traceback (most recent call last)<br>
----&gt; 4 log_dict = model.train(nn.MSELoss() , epochs=1, batch_size=15,<br>
5                        training_set=training_data, validation_set=validation_data,<br>
6                        testset=test_data)</p>
<p>8 frames<br>
/usr/local/lib/python3.8/dist-packages/torchvision/transforms/functional.py in to_tensor(pic)<br>
135         _log_api_usage_once(to_tensor)<br>
136     if not (F_pil._is_pil_image(pic) or _is_numpy(pic)):<br>
 → 137         raise TypeError(f"pic should be PIL Image or ndarray. Got {type(pic)}")<br>
138<br>
139     if _is_numpy(pic) and not _is_numpy_image(pic):</p>
<p>TypeError: pic should be PIL Image or ndarray. Got &lt;class ‘str’&gt;</p>