<jupyter_start><jupyter_text>Model<jupyter_code>class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        def conv_bn(inp, oup, stride):
            return nn.Sequential(
                nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
                nn.BatchNorm2d(oup),
                nn.ReLU(inplace=True)
            )

        def conv_dw(inp, oup, stride):
            return nn.Sequential(
                nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
                nn.BatchNorm2d(inp),
                nn.ReLU(inplace=True),
    
                nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
                nn.BatchNorm2d(oup),
                nn.ReLU(inplace=True),
            )

        self.model = nn.Sequential(
            conv_bn(  3,  32, 2), 
            conv_dw( 32,  64, 1),
            conv_dw( 64, 128, 2),
            conv_dw(128, 128, 1),
            conv_dw(128, 256, 2),
            conv_dw(256, 256, 1),
            conv_dw(256, 512, 2),
            conv_dw(512, 512, 1),
            conv_dw(512, 512, 1),
            conv_dw(512, 512, 1),
            conv_dw(512, 512, 1),
            conv_dw(512, 512, 1),
            conv_dw(512, 1024, 2),
            conv_dw(1024, 1024, 1),
            nn.AvgPool2d(3),
        )
        self.fc = nn.Linear(1024, 9)

    def forward(self, x):
        x = self.model(x)
        x = x.view(-1, 1024)
        x = self.fc(x)
        return F.log_softmax(x, dim=1)
torch.manual_seed(seed)

use_cuda = not no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
model = Net().to(device)
summary(model, input_size=(3,128,128))
train_dir = 'dataset/tomato/'
# test_dir = 'dataset/mnist/testing/'
train_dataset = datasets.ImageFolder(root=train_dir,
                                    transform=transforms.Compose([
                                        transforms.ToTensor(),
                                        transforms.Normalize((0.1307,), (0.3081,)),
                                    ]))
# test_dataset = datasets.ImageFolder(root=test_dir,
#                                     transform=transforms.Compose([
#                                         transforms.ToTensor(),
#                                         transforms.Normalize((0.1307,), (0.3081,))
#                                     ]))
# train_loader = torch.utils.data.DataLoader(train_dataset,
#                                           batch_size = batch_size,
#                                           shuffle = True)
# test_loader = torch.utils.data.DataLoader(test_dataset,
#                                           batch_size = batch_size,
#                                           shuffle = False)
train_loader = torch.utils.data.DataLoader(train_dataset,batch_size = len(train_dataset.imgs))
for image, label in train_loader:
    image = image
    label = label
    print(image.shape)
    print(label.shape)
data = image.view(-1,128*128*3)
target = label.view(-1,1)
dataset_x = data.numpy()
dataset_y = target.numpy().astype(int)
import numpy as np
np.save('./dataset/image.npy', dataset_x)
np.save('./dataset/label.npy', dataset_y)
import numpy as np
dataset_x = np.load('./dataset/image.npy')
dataset_y = np.load('./dataset/label.npy')
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(dataset_x, dataset_y, test_size=0.1, random_state=1,stratify=dataset_y)

X_train.shape
y_train.shape
y_train[0:100,0]
X_train = torch.FloatTensor(X_train).view([-1,3,128,128])
y_train = torch.LongTensor(y_train).view([-1])
X_val = torch.FloatTensor(X_val).view([-1,3,128,128])
y_val = torch.LongTensor(y_val).view([-1])

y_val.bincount(), y_train.bincount()
X_train.shape, y_train.shape
trainDataset = torch.utils.data.TensorDataset(X_train, y_train)
validDataset = torch.utils.data.TensorDataset(X_val, y_val)
Train_loader = torch.utils.data.DataLoader(trainDataset,
                                          batch_size = batch_size,
                                          shuffle = False)
Valid_loader = torch.utils.data.DataLoader(validDataset,
                                          batch_size = len(X_val),
                                          shuffle = False)
from datetime import datetime
print(datetime.now())
from torch.optim.lr_scheduler import ReduceLROnPlateau
import torchvision
from torch.utils.tensorboard import SummaryWriter
lr = 0.002
epochs = 100
model = Net().to(device)
optimizer = optim.SGD(model.parameters(),lr=lr,momentum=momentum)
loss_func = nn.CrossEntropyLoss()
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.9, patience=1, verbose=True)
images, labels = next(iter(Train_loader))
writer = SummaryWriter(comment='Model')
writer.add_graph(Net(), input_to_model=images)
writer.close()
writer_t = SummaryWriter(comment=' Train / epoch : {} / learning_rate : {}'.format(epochs,lr))
writer_v = SummaryWriter(comment=' Val / epoch : {} / learning_rate : {}'.format(epochs,lr))

correct_train = 0
total_train = 0
for i in range(epochs):
    start_time = datetime.now()
    for j, (data, label) in enumerate(Train_loader):
        x = data.to(device)
        y_ = label.to(device)

        optimizer.zero_grad()
        output = model.forward(x)
        loss = loss_func(output, y_)
        loss.backward()
        optimizer.step()
#         print(output.max().shape,y_.shape)
        _, predicted = torch.max(output.data, 1)
        correct_train += (predicted == y_).sum().item()
        total_train += y_.size(0)
        accuracy_train = (100 * correct_train / total_train)
#         print('a')
        
        if (j+1) % 50 == 0:
            print('Epoch:{} / batch:{} / loss {:.2f}'.format(i+1,j+1, loss))
    with torch.no_grad():
        for val_x, val_y in Valid_loader:
            val_x = val_x.to(device)
            val_y = val_y.to(device)
            val_output = model.forward(val_x)
            v_loss = loss_func(val_output, val_y)
            _, predicted_val = torch.max(val_output.data, 1)
            accuracy_val = ( 100 * (predicted_val == val_y).sum().item() / len(val_x))
    end_time = datetime.now()    
    print('Epoch {} / time {} / loss {:.2f} / val_loss {:.2f} / acc {:.2f} / val_acc {:.2f}'.format(i+1, end_time - start_time, loss, v_loss, accuracy_train, accuracy_val))
    scheduler.step(v_loss, i)
#     if i == 0:
#       grid = torchvision.utils.make_grid(data)
#       writer.add_image('images',grid, i)
    writer_t.add_scalar('Loss', loss, i+1)
    writer_v.add_scalar('Loss', v_loss, i+1)
    writer_t.add_scalar('Acc', accuracy_train, i+1)
    writer_v.add_scalar('Acc', accuracy_val, i+1)

writer_t.close()
writer_v.close()

correct = 0
total = 0

with torch.no_grad():
    for image, label in test_loader:
        x = image.to(device)
        y_ = label.to(device)
        
        output = model.forward(x)
        _, output_index = torch.max(output,1)
        
        total += label.size(0)
        correct += (output_index == y_).sum().float()
        
    print("Accuracy of Test Data: {}".format(100*correct/total))
import tensorflow as tf<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13397732045359093, 'fraction_numerical': 0.03205935881282374, 'mean_word_length': 2.165263624280018, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '544255', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2310, 'n_words': 434, 'path': 'Project/CNN_tomato/data_from_image_folder.ipynb', 'repo_name': 'sung429/Python', 'size': '7143'}