Assuming 'img1.png.jpeg' was the last uploaded image and is in the current directory
if os.path.exists('img1.png.jpeg'):
shutil.move('img1.png.jpeg', os.path.join(image_dir, 'img1.png.jpeg'))
print(f"Moved img1.png.jpeg to {image_dir}/")
else:
print("img1.png.jpeg not found in current directory.")
Move the dummy mask to the mask directory
if os.path.exists('dummy_mask.png'):
shutil.move('dummy_mask.png', os.path.join(mask_dir, 'dummy_mask.png'))
print(f"Moved dummy_mask.png to {mask_dir}/")
else:
print("dummy_mask.png not found in current directory.")
Now, re-run the cell where you instantiate the dataset (cell q-SoLWWz65Ij) and the training loop.
!pip install torch torchvision segmentation-models-pytorch
import torch
import segmentation_models_pytorch as smp
import segmentation_models_pytorch as smp
model = smp.Unet(
encoder_name="resnet34",
encoder_weights="imagenet",
classes=10,
activation=None
)
for fn in uploaded.keys():
print(f'User uploaded file "{fn}" with length {len(uploaded[fn])} bytes')
[13/03, 14:49] Lilyput: from google.colab import files
uploaded = files.upload()
for fn in uploaded.keys():
print(f'User uploaded file "{fn}" with length {len(uploaded[fn])} bytes')
import os
import cv2
import numpy as np
assuming image_paths is a list of image file paths( e.g., from uploadded files)
add mask_paths is a corresponding list of mask file paths
#example:using the previously uploaded image
image_paths =["img1.png.jpeg"]
#create a dummy mask file for demonstration if it doesn't exist
dummy_mask_path = "dummy_mask.png"
if not os.path.exists(dummy_mask_path):
dummy_mask_data = np.zeros((256, 256), dtype=np.uint8)
cv2.imwrite(dummy_mask_path, dummy_mask_data)
print("Dummy mask file created:", dummy_mask_path)
mask_paths = [dummy_mask_path]
try:
dataset=SegmentationDataset(image_paths,mask_paths)
print("Dataset created successfully")
print(f"number of item in dataser:{len(dataset)}")
if len(dataset)>0:
first_image, first_mask = dataset[0]
print("First image shape:", first_image.shape)
print("First mask shape:", first_mask.shape)
except Exception as e:
print(f"Error creating dataset: {e}")
import os
import cv2
import torch
from torch.utils.data import Dataset
Assuming 'img1.png.jpeg' was the last uploaded image and is in the current directory
if os.path.exists('img1.png.jpeg'):
shutil.move('img1.png.jpeg', os.path.join(image_dir, 'img1.png.jpeg'))
print(f"Moved img1.png.jpeg to {image_dir}/")
else:
print("img1.png.jpeg not found in current directory.")
Move the dummy mask to the mask directory
if os.path.exists('dummy_mask.png'):
shutil.move('dummy_mask.png', os.path.join(mask_dir, 'dummy_mask.png'))
print(f"Moved dummy_mask.png to {mask_dir}/")
else:
print("dummy_mask.png not found in current directory.")
# Now, re-run the cell where you instantinate the dataset (cell q-SoLWWz65Ij) and the training
import segmentation_models_pytorch as smp
model = smp.Unet(
encoder_name="resnet34",
encoder_weights="imagenet",
classes=10,
activation=None
)
model.eval()
Define a dummy test_image for demonstration
Example: A single random image with 3 channels and 256x256 dimensions
test_image = torch.randn(1, 3, 256, 256)
If you want to use a real image from the dataset, you could do: