<jupyter_start><jupyter_text>Model<jupyter_code>class Autoencoder(nn.Module):
    
    def __init__(self):
        super(Autoencoder, self).__init__()
        self.encoder1 = nn.Conv2d(3, 16, 3, padding=1)
        self.encoder2 = nn.Conv2d(16, 8, 3, padding=1)
        self.encoder3 = nn.Conv2d(8, 4, 3, padding=1)
        self.encoder4 = nn.Conv2d(4, 4, 3, padding=1)
        
        self.pool = nn.MaxPool2d(2, ceil_mode=True)
        self.up1 = nn.Upsample(75, mode='nearest')
        self.up = nn.Upsample(scale_factor=2, mode='nearest')
        
        self.decoder1 = nn.Conv2d(4, 4, 3, padding=1)
        self.decoder2 = nn.Conv2d(4, 4, 3, padding=1)
        self.decoder3 = nn.Conv2d(4, 8, 3, padding=1)
        self.decoder4 = nn.Conv2d(8, 16, 3, padding=1)
        self.decoder5 = nn.Conv2d(16, 3, 3, padding=1)
        
    def forward(self, x):
        x = F.relu(self.encoder1(x))
        x = self.pool(x)
        x = F.relu(self.encoder2(x))
        x = self.pool(x)
        x = F.relu(self.encoder3(x))
        x = self.pool(x)
        x = F.relu(self.encoder4(x))
        encoder = self.pool(x)
        
        x = F.relu(self.decoder1(encoder))
        x = self.up1(x)
        x = F.relu(self.decoder2(x))
        x = self.up(x)
        x = F.relu(self.decoder3(x))
        x = self.up(x)
        x = F.relu(self.decoder4(x))
        x = self.up(x)
        decoder = F.relu(self.decoder5(x))
        
        return decoder
model = Autoencoder()
model
with torch.no_grad():
    model.eval()
    img = torch.rand(6,3,600,600)
    print(model(img).shape)<jupyter_output>torch.Size([6, 3, 600, 600])<jupyter_text>Dataset<jupyter_code>import os
import glob

from torch.utils.data import DataLoader, Dataset
from PIL import Image

class myDataset(Dataset):
    
    def __init__(self, image_path, transform=None):
        self.image_path = image_path
        self.transform = transform
        self.image_list = glob.glob(image_path + '/*')
        
    def __len__(self):
        
        return len(self.image_list)
    
    def __getitem__(self, idx):
        file_name = self.image_list[idx]
        
        image = Image.open(file_name)
        
        if self.transform:
            image = self.transform(image)
            
        return image, image  

def collate_fn(batch):
    return tuple(zip(*batch))
import torchvision.transforms as transforms

transform = transforms.Compose([
    transforms.Resize((600,600)),
    transforms.Grayscale(3),
    transforms.ToTensor()
])

train_path = r'C:\Users\gjust\Documents\Github\data\fruit\apple'
test_path = r'C:\Users\gjust\Documents\Github\data\fruit\apple'

trainset = myDataset(train_path, transform=transform)
testset = myDataset(test_path, transform=transform)

trainloader = DataLoader(trainset, batch_size=4)
testloader = DataLoader(testset, batch_size=4)

inputs, outputs = iter(trainloader).next()<jupyter_output><empty_output><jupyter_text>GPU<jupyter_code>device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
print(device)<jupyter_output>cuda<jupyter_text>Optimizer<jupyter_code>optimizer = optim.Adam(model.parameters(), lr=0.001)
loss_func = nn.MSELoss()
loss_func<jupyter_output><empty_output><jupyter_text>학습<jupyter_code>EPOCH = 5
train_epoch_loss = []
val_peoch_loss = []

for e in range(EPOCH):
    
    # Train
    model.train()
    train_iter_loss = []
    for i, (images, labels) in enumerate(trainloader):
        images, labels = images.to(device), labels.to(device)
        
        optimizer.zero_grad()
        outputs = model(images)
        loss = loss_func(outputs, labels)
        loss.backward()
        optimizer.step()
        
        train_iter_loss.append(loss.item())
        
    train_epoch_loss.append(sum(train_iter_loss))
    print(f'Train Epoch[{e+1}/{EPOCH}] / Loss : {loss}')
    
    # Validation
    
    if e+1 == 5:
        model.eval()
        val_iter_loss = []
        for i, (images, labels) in enumerate(testloader):
            images, labels = images.to(device), labels.to(device)

            outputs = model(images)
            loss = loss_func(outputs, labels)
            val_iter_loss.append(loss.item())

        val_peoch_loss.append(sum(val_iter_loss))
        print(f'Validation Epoch[{e+1}/{EPOCH}] / Loss : {loss}')<jupyter_output>Train Epoch[1/5] / Loss : 0.005480456631630659
Train Epoch[2/5] / Loss : 0.00514173461124301
Train Epoch[3/5] / Loss : 0.005242711864411831
Train Epoch[4/5] / Loss : 0.005308091174811125
Train Epoch[5/5] / Loss : 0.004775404930114746
Validation Epoch[5/5] / Loss : 0.004396789241582155
{'fraction_non_alphanumeric': 0.13436748962202316, 'fraction_numerical': 0.05090670745029495, 'mean_word_length': 2.7803468208092488, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '501262', 'license': 'MIT', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1672, 'n_words': 281, 'path': '4. Project/Abnormal/Abnormal.ipynb', 'repo_name': 'gjustin40/Pytorch', 'size': '4577'}