{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#### Hi\nthis will be a CIFAR10 using VGG16 (pre trained model)  \nMy model (hand made) (the previous one) had 82.2% of accuracy.   \nLets see how this VGG16 will do  \n\n2023/01/30 2027i let get init  ","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:51.572417Z","iopub.execute_input":"2023-07-12T10:59:51.572796Z","iopub.status.idle":"2023-07-12T10:59:51.581012Z","shell.execute_reply.started":"2023-07-12T10:59:51.572765Z","shell.execute_reply":"2023-07-12T10:59:51.579826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nimport torchvision.transforms as transforms\nimport torch.optim as optim\nimport time\nimport torch.nn.functional as F\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nfrom torchvision import models\nimport sys","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:51.583131Z","iopub.execute_input":"2023-07-12T10:59:51.584339Z","iopub.status.idle":"2023-07-12T10:59:51.592871Z","shell.execute_reply.started":"2023-07-12T10:59:51.584309Z","shell.execute_reply":"2023-07-12T10:59:51.591703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GPU\nimport torch\n\nif torch.cuda.is_available():\n    device = 'cuda'\n    device_count = torch.cuda.device_count()\n    if device_count > 1:\n        # GPU's\n        templist = [1, 2, 3]\n        templist = torch.FloatTensor(templist).to(device)\n        print(\"Cuda torch working: \", end=\"\")\n        print(templist.is_cuda)\n        print(\"GPU device count: \", end=\"\")\n        print(device_count)\n        for i in range(device_count):\n            print(\"GPU name {}: {}\".format(i, torch.cuda.get_device_name(i)))\n        print(\"current device no.: \", end=\"\")\n        print(torch.cuda.current_device())\n    else:\n        # A GPU\n        print(\"Cuda torch working: \", end=\"\")\n        print(torch.cuda.is_available())\n        print(\"GPU device count: \", end=\"\")\n        print(device_count)\n        print(\"GPU name: \", end=\"\")\n        print(torch.cuda.get_device_name(0))\n        print(\"current device no.: \", end=\"\")\n        print(torch.cuda.current_device())\nelse:\n    if torch.backends.mps.is_available():\n        print(\"Apple device detected\\nActivating Apple Silicon GPU\")\n        device = torch.device(\"mps\")\n    else:\n        print(\"Cannot use GPU, activating CPU\")\n        device = 'cpu'\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:51.596308Z","iopub.execute_input":"2023-07-12T10:59:51.597508Z","iopub.status.idle":"2023-07-12T10:59:51.610070Z","shell.execute_reply.started":"2023-07-12T10:59:51.597464Z","shell.execute_reply":"2023-07-12T10:59:51.609004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Data\nsince the VGG16 uses 224 x 224 model and CIFAR10 is 32x32 we have to change the CIFAR10 size to 224x224  ","metadata":{}},{"cell_type":"code","source":"from torchvision import models\n\nnum_subprocesses = 8 ###CHANGE (REDUCE) VALUE IF ERROR (its about GPU memroy)###\ntransform = transforms.Compose(\n    [transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\n\ntrainset = torchvision.datasets.CIFAR10(root='./data', train=True,\n                                        download=True, transform=transform)\ntrainloader = torch.utils.data.DataLoader(trainset, batch_size=32,\n                                        shuffle=True, num_workers=num_subprocesses)\ntestset = torchvision.datasets.CIFAR10(root='./data', train=False,\n                                        download=True, transform=transform)\ntestloader = torch.utils.data.DataLoader(testset, batch_size=32,\n                                        shuffle=False, num_workers=num_subprocesses)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:51.612610Z","iopub.execute_input":"2023-07-12T10:59:51.613232Z","iopub.status.idle":"2023-07-12T10:59:53.235964Z","shell.execute_reply.started":"2023-07-12T10:59:51.613201Z","shell.execute_reply":"2023-07-12T10:59:53.234946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom torchvision import datasets\n\n# 각 클래스의 이미지 수 계산\nclass_counts = np.bincount(trainset.targets)\n\n# 클래스 이름 가져오기\nclass_names = trainset.classes\n\n# 데이터 시각화\nplt.figure(figsize=(12,6))\nax = sns.barplot(x=class_names, y=class_counts)\nplt.xlabel('Classes')\nplt.ylabel('Number of Images')\nplt.title('Number of Images per Class in the Training Set')\n\n# 각 막대에 텍스트 추가\nfor i in range(len(class_counts)):\n    ax.text(i, class_counts[i], class_counts[i], ha='center')\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:53.237587Z","iopub.execute_input":"2023-07-12T10:59:53.237975Z","iopub.status.idle":"2023-07-12T10:59:53.599061Z","shell.execute_reply.started":"2023-07-12T10:59:53.237943Z","shell.execute_reply":"2023-07-12T10:59:53.598055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### VGG\nI will use the predtrained vgg  \nThe default out features of vgg16 is 1000. But we need only one.  \nSo we will need line no 5. to change the out features to 10  \nAnd since we will not change the parameters we will not require grad  \n  \n","metadata":{}},{"cell_type":"code","source":"vgg16 = models.vgg16(pretrained=True)\n\n# if multiple GPUs are available, wrap model with DataParallel \nif torch.cuda.device_count() > 1:\n    print(\"Using\", torch.cuda.device_count(), \"GPUs\")\n    vgg16 = nn.DataParallel(vgg16)\n\nvgg16.to(device)\n\n\n# change the number of classes \nvgg16.module.classifier[6].out_features = 10\n\n# freeze convolution weights\n# BUT convolutional layers WILL learn // allowing the model to focus on learning the mapping between the extracted features and the output predictions\nfor param in vgg16.module.features.parameters():\n    param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:53.600603Z","iopub.execute_input":"2023-07-12T10:59:53.601231Z","iopub.status.idle":"2023-07-12T10:59:55.316865Z","shell.execute_reply.started":"2023-07-12T10:59:53.601195Z","shell.execute_reply":"2023-07-12T10:59:55.315731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### LOSS and Optim\n","metadata":{}},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(vgg16.module.classifier.parameters(), lr=0.001, momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:55.320177Z","iopub.execute_input":"2023-07-12T10:59:55.320531Z","iopub.status.idle":"2023-07-12T10:59:55.327584Z","shell.execute_reply.started":"2023-07-12T10:59:55.320503Z","shell.execute_reply":"2023-07-12T10:59:55.326507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#validation function\ndef validate(model, test_dataloader):\n    model.eval()\n    val_running_loss = 0.0\n    val_running_correct = 0\n    for int, data in enumerate(test_dataloader):\n        data, target = data[0].to(device), data[1].to(device)\n        output = model(data)\n        loss = criterion(output, target)\n        \n        val_running_loss += loss.item()\n        _, preds = torch.max(output.data, 1)\n        val_running_correct += (preds == target).sum().item()\n    \n    val_loss = val_running_loss/len(test_dataloader.dataset)\n    val_accuracy = 100. * val_running_correct/len(test_dataloader.dataset)\n    \n    return val_loss, val_accuracy","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:55.329228Z","iopub.execute_input":"2023-07-12T10:59:55.329626Z","iopub.status.idle":"2023-07-12T10:59:55.337707Z","shell.execute_reply.started":"2023-07-12T10:59:55.329593Z","shell.execute_reply":"2023-07-12T10:59:55.336531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training function\ndef train(model, train_dataloader):\n    model.train()\n    train_running_loss = 0.0\n    train_running_correct = 0\n    for i, data in enumerate(train_dataloader):\n        data, target = data[0].to(device), data[1].to(device)\n        optimizer.zero_grad()\n        output = model(data)\n        loss = criterion(output, target)\n        train_running_loss += loss.item()\n        _, preds = torch.max(output.data, 1)\n        train_running_correct += (preds == target).sum().item()\n        loss.backward()\n        optimizer.step()\n    train_loss = train_running_loss/len(train_dataloader.dataset)\n    train_accuracy = 100. * train_running_correct/len(train_dataloader.dataset)\n\n    #print('Train Loss: %.4f'%train_loss)\n    \n    return train_loss, train_accuracy","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:55.339591Z","iopub.execute_input":"2023-07-12T10:59:55.340122Z","iopub.status.idle":"2023-07-12T10:59:55.349591Z","shell.execute_reply.started":"2023-07-12T10:59:55.340085Z","shell.execute_reply":"2023-07-12T10:59:55.348518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Do you want to skip leanring or not?\n### Set SKIP to 1 if you want to skip leaning \n\nSKIP = 0\nNB_EPOCH = 10\n\nif (SKIP == 0):\n\n    # Set the initial best validation accuracy to 0\n    best_val_accuracy = 0.0\n\n    # Initialize the best model state dictionary\n    best_model_state_dict = None\n\n    train_loss , train_accuracy = [], []\n    val_loss , val_accuracy = [], []\n    start = time.time()\n    for epoch in range(NB_EPOCH):\n        train_epoch_loss, train_epoch_accuracy = train(vgg16, trainloader)\n        val_epoch_loss, val_epoch_accuracy = validate(vgg16, testloader)\n        print(\"Train Loss : %f/  Train Acc : %.2f/  Val Loss : %f/  Val Acc : %.2f\"%(train_epoch_loss,train_epoch_accuracy,val_epoch_loss,val_epoch_accuracy))\n        train_loss.append(train_epoch_loss)\n        train_accuracy.append(train_epoch_accuracy)\n        val_loss.append(val_epoch_loss)\n        val_accuracy.append(val_epoch_accuracy)\n\n        #print(\"Train Acc : %f\"%train_accuracy,\"Val Acc : %f\"%val_accuracy)\n            # Check if the current validation accuracy is better than the previous best\n        if val_epoch_accuracy > best_val_accuracy:\n            # Update the best validation accuracy\n            best_val_accuracy = val_epoch_accuracy\n\n            # Save the state dictionary of the current best model\n            best_model_state_dict = vgg16.state_dict()\n\n\n            \n    # Save the best model state dictionary to a file\n    torch.save(best_model_state_dict, \"vgg16CIFAR.pth\")\n        \n    end = time.time()\n\n    print(int((end-start)//60), 'minutes', int((end-start)%60), 'seconds')\nelse:\n    print(\"skip\")","metadata":{"execution":{"iopub.status.busy":"2023-07-12T10:59:55.351111Z","iopub.execute_input":"2023-07-12T10:59:55.351505Z","iopub.status.idle":"2023-07-12T11:06:11.707266Z","shell.execute_reply.started":"2023-07-12T10:59:55.351471Z","shell.execute_reply":"2023-07-12T11:06:11.706016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 7))\nplt.plot(train_accuracy, color='green', label='train accuracy')\nplt.plot(val_accuracy, color='blue', label='validataion accuracy')\nplt.legend()\nplt.savefig('accuracy.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T11:06:11.709021Z","iopub.execute_input":"2023-07-12T11:06:11.709666Z","iopub.status.idle":"2023-07-12T11:06:12.074752Z","shell.execute_reply.started":"2023-07-12T11:06:11.709621Z","shell.execute_reply":"2023-07-12T11:06:12.073856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 7))\nplt.plot(train_loss, color='orange', label='train loss')\nplt.plot(val_loss, color='red', label='validataion loss')\nplt.legend()\nplt.savefig('loss.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T11:06:12.077809Z","iopub.execute_input":"2023-07-12T11:06:12.078093Z","iopub.status.idle":"2023-07-12T11:06:12.434265Z","shell.execute_reply.started":"2023-07-12T11:06:12.078069Z","shell.execute_reply":"2023-07-12T11:06:12.433340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Time\n\nimport time\n\ndef format_time(seconds):\n    hours = seconds // 3600\n    minutes = (seconds % 3600) // 60\n    seconds = seconds % 60\n    if hours > 0:\n        return f\"{int(hours)}h {int(minutes)}m {seconds:.2f}s\"\n    elif minutes > 0:\n        return f\"{int(minutes)}m {seconds:.2f}s\"\n    else:\n        return f\"{seconds:.2f}s\"","metadata":{"execution":{"iopub.status.busy":"2023-07-12T11:06:12.436735Z","iopub.execute_input":"2023-07-12T11:06:12.437468Z","iopub.status.idle":"2023-07-12T11:06:12.443744Z","shell.execute_reply.started":"2023-07-12T11:06:12.437435Z","shell.execute_reply":"2023-07-12T11:06:12.442717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Kaggle files","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"To unzip\n","metadata":{}},{"cell_type":"code","source":"## NEW!\n## BTW this takes a long long time (like 3 hours....)\n##\nimport os\nimport subprocess\n\nif 'KAGGLE_KERNEL_RUN_TYPE' in os.environ:\n    # Running on Kaggle\n    file_path = \"/kaggle/input/cifar-10/test.7z\"\n    output_path = \"/kaggle/working/test/test/\"\n    print(\"Running on Kaggle\")\nelse:\n    # Running on local machine\n    file_path = \"test.7z\"  # change to your local file path\n    output_path = \"./test/\"  # change to your desired output path\n    print(\"Running on local machine\")\n\n# Check if output directory exists\nif not os.path.exists(output_path):\n    print(\"Output directory does not exist. Creating directory and extracting files...\")\n    # Directory does not exist, extract files\n    os.makedirs(output_path, exist_ok=True)\n    if 'KAGGLE_KERNEL_RUN_TYPE' in os.environ:\n        subprocess.run(['7za', 'x', file_path, f'-o{output_path}'])\n    else:\n        import py7zr\n        with py7zr.SevenZipFile(file_path, mode='r') as z:\n            z.extractall(output_path)\nelse:\n    # Directory exists, check if it's empty\n    if not os.listdir(output_path):\n        print(\"Output directory exists but is empty. Extracting files...\")\n        # Directory is empty, extract files\n        if 'KAGGLE_KERNEL_RUN_TYPE' in os.environ:\n            subprocess.run(['7za', 'x', file_path, f'-o{output_path}'])\n        else:\n            import py7zr\n            with py7zr.SevenZipFile(file_path, mode='r') as z:\n                z.extractall(output_path)\n    else:\n        print(\"Output directory exists and is not empty. No extraction needed.\")\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T11:06:16.348098Z","iopub.execute_input":"2023-07-12T11:06:16.348471Z","iopub.status.idle":"2023-07-12T15:30:16.759814Z","shell.execute_reply.started":"2023-07-12T11:06:16.348436Z","shell.execute_reply":"2023-07-12T15:30:16.757589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load model","metadata":{}},{"cell_type":"code","source":"\ntry:\n    # Load the pre-trained weights\n    model_checkpoint = \"vgg16CIFAR.pth\"\n    vgg16.load_state_dict(torch.load(model_checkpoint))\n\n    # Make sure to do this before inference\n    vgg16.eval()\n    vgg16.to(device)\n    print(\"model loaded\")\n    \nexcept NameError:\n    from torchvision import models\n    import torch\n    print(\"You skipped the training, loading vgg16 bset vgg16 model\")\n    vgg16 = models.vgg16(pretrained='True')\n    # if multiple GPUs are available, wrap model with DataParallel \n    if torch.cuda.device_count() > 1:\n        print(\"Using\", torch.cuda.device_count(), \"GPUs\")\n        vgg16 = nn.DataParallel(vgg16)\n    vgg16.to(device)\n    # change the number of classes \n    vgg16.classifier[6].out_features = 10\n    # freeze convolution weights\n    # BUT convolutional layers WILL learn // allowing the model to focus on learning the mapping between the extracted features and the output predictions\n    for param in vgg16.features.parameters():\n        param.requires_grad = False\n    # Load the pre-trained weights\n    model_checkpoint = \"vgg16CIFAR.pth\"\n    vgg16.load_state_dict(torch.load(model_checkpoint))\n    # Make sure to do this before inference\n    vgg16.eval()\n    vgg16.to(device)\n    print(\"Model loaded\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {str(e)}\")\n\n\n#print(vgg16)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T15:30:16.765057Z","iopub.execute_input":"2023-07-12T15:30:16.765363Z","iopub.status.idle":"2023-07-12T15:30:17.433687Z","shell.execute_reply.started":"2023-07-12T15:30:16.765331Z","shell.execute_reply":"2023-07-12T15:30:17.432639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load path","metadata":{}},{"cell_type":"code","source":"# Path to your new images\nimage_path = output_path + \"test/\"\nimage_files = os.listdir(image_path)\nprint(\"Loaded path\")","metadata":{"execution":{"iopub.status.busy":"2023-07-12T15:30:17.435232Z","iopub.execute_input":"2023-07-12T15:30:17.435572Z","iopub.status.idle":"2023-07-12T15:30:17.615943Z","shell.execute_reply.started":"2023-07-12T15:30:17.435539Z","shell.execute_reply":"2023-07-12T15:30:17.614980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image inspection 300,000","metadata":{}},{"cell_type":"code","source":"import sys\n\n# Path to your new images\nimage_path = output_path + \"test\"\nimage_files = os.listdir(image_path)\nprint(\"Loaded path\")\n\n# Get the number of image files\nnum_images = len(image_files)\n\n# Check if the image count is 300000\nif num_images != 300000:\n    print(f\"Error: The number of images (files) is {num_images}, not 300,000. Please check the dataset.\")\n    sys.exit(0)\nelse:\n    print(f\"Number of images is correct: {num_images}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T15:30:17.617216Z","iopub.execute_input":"2023-07-12T15:30:17.618190Z","iopub.status.idle":"2023-07-12T15:30:17.802682Z","shell.execute_reply.started":"2023-07-12T15:30:17.618154Z","shell.execute_reply":"2023-07-12T15:30:17.801675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict ramdom 20 images\nimport random\nfrom torchvision.utils import make_grid\nfrom PIL import Image\n\nclasses = ('airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')\n\n\n\n\n# choose 20 random images from test set\nrandom_images = random.sample(image_files, 20)\n\nfig, axs = plt.subplots(5, 4, figsize=(20, 20))\naxs = axs.ravel()\n\n# Perform inference and display images with predicted labels\nfor i, image_file in enumerate(random_images):\n    # Open image\n    image = Image.open(os.path.join(image_path, image_file))\n\n    # Apply the same transformations as you did for your training images\n    image_tensor = transform(image).float()\n\n    # Add an extra batch dimension since pytorch treats all images as batches\n    image_tensor = image_tensor.unsqueeze_(0).to(device)\n\n    # Perform inference\n    outputs = vgg16(image_tensor)\n    _, predicted = torch.max(outputs, 1)\n\n    # Display images with predicted labels\n    image_tensor = image_tensor.cpu().numpy().transpose((0, 2, 3, 1))\n    image_tensor = image_tensor.squeeze() * 0.5 + 0.5  # Un-normalize\n    axs[i].imshow(image_tensor)\n    axs[i].set_title(classes[predicted.item()])\n    axs[i].axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T15:30:17.803966Z","iopub.execute_input":"2023-07-12T15:30:17.804290Z","iopub.status.idle":"2023-07-12T15:30:22.258131Z","shell.execute_reply.started":"2023-07-12T15:30:17.804259Z","shell.execute_reply":"2023-07-12T15:30:22.256776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict fist 20 images\nimport os\nfrom torchvision.utils import make_grid\n\nclasses = ('airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')\n\n# Define first 20 images from the dataset\nimage_files = [f\"{i+1}.png\" for i in range(20)]\n\nfig, axs = plt.subplots(5, 4, figsize=(20, 20))\naxs = axs.ravel()\n\n# Perform inference and display images with predicted labels\nfor i, image_file in enumerate(image_files):\n    # Open image\n    image = Image.open(os.path.join(image_path, image_file))\n\n    # Apply the same transformations as you did for your training images\n    image_tensor = transform(image).float()\n\n    # Add an extra batch dimension since pytorch treats all images as batches\n    image_tensor = image_tensor.unsqueeze_(0).to(device)\n\n    # Perform inference\n    outputs = vgg16(image_tensor)\n    _, predicted = torch.max(outputs, 1)\n\n    # Print predicted labels\n    print(f'Image: {image_file}, Label: {classes[predicted.item()]}')\n\n    # Display images with predicted labels\n    image_tensor = image_tensor.cpu().numpy().transpose((0, 2, 3, 1))\n    image_tensor = image_tensor.squeeze() * 0.5 + 0.5  # Un-normalize\n    axs[i].imshow(image_tensor)\n    axs[i].set_title(classes[predicted.item()])\n    axs[i].axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:02:16.603011Z","iopub.execute_input":"2023-07-12T16:02:16.603438Z","iopub.status.idle":"2023-07-12T16:02:20.622300Z","shell.execute_reply.started":"2023-07-12T16:02:16.603403Z","shell.execute_reply":"2023-07-12T16:02:20.617780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test to csv no batch\n# We have Batch verstion (which is more fatser) so you can skip this if you want\nTemp_Skip =1\n\nif Temp_Skip == 0:\n\n    import os\n    import pandas as pd\n    import time\n    from torchvision.utils import make_grid\n\n    classes = ('airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')\n\n    # Define first 20 images from the dataset\n    image_files = [f\"{i+1}.png\" for i in range(300000)]\n\n    # Prepare to record the results\n    results = []\n\n    start_time = time.time()\n\n    for i, image_file in enumerate(image_files):\n        # Open image\n        image = Image.open(os.path.join(image_path, image_file))\n\n        # Apply the same transformations as you did for your training images\n        image_tensor = transform(image).float()\n\n        # Add an extra batch dimension since pytorch treats all images as batches\n        image_tensor = image_tensor.unsqueeze_(0).to(device)\n\n        # Perform inference\n        outputs = vgg16(image_tensor)\n        _, predicted = torch.max(outputs, 1)\n\n        # Append to results\n        results.append([image_file, classes[predicted.item()]])\n\n        # Print time estimation\n        elapsed_time = time.time() - start_time\n        estimated_remaining_time = elapsed_time / (i + 1) * (len(image_files) - i - 1)\n        print(f\"Progress: {i+1}/{len(image_files)}, Estimated time left: {format_time(estimated_remaining_time)}\",end='\\r')\n\n    # Save results to csv file\n    df = pd.DataFrame(results, columns=['id', 'label'])\n    df.to_csv('sampleSubmission.csv', index=False)\n    print(\"CSV file done!\")\nelse:\n    print(\"Skip Nobatch\")","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:02:21.659964Z","iopub.execute_input":"2023-07-12T16:02:21.660338Z","iopub.status.idle":"2023-07-12T16:02:21.673154Z","shell.execute_reply.started":"2023-07-12T16:02:21.660307Z","shell.execute_reply":"2023-07-12T16:02:21.671728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Batch Test to csv Batch\n\nimport os\nimport pandas as pd\nimport time\nimport torch\nfrom torchvision.utils import make_grid\n\nclasses = ('airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')\n\n# Define first 20 images from the dataset\nimage_files = [f\"{i+1}.png\" for i in range(300000)]\n\n# Prepare to record the results\nresults = []\n\n# Define your batch size. Be aware that a larger batch size requires more memory.\nbatch_size = 32\n\nstart_time = time.time()\n\nnum_batches = len(image_files) // batch_size + 1\n\nprint(\"Current deivce\",device)\n\nfor b in range(num_batches):\n\n    batch_files = image_files[b*batch_size: (b+1)*batch_size]\n\n    if not batch_files:  # Skip the loop if there are no batch files\n        continue\n\n    batch_tensors = []\n    for image_file in batch_files:\n        # Open image\n        image = Image.open(os.path.join(image_path, image_file))\n\n        # Apply the same transformations as you did for your training images\n        image_tensor = transform(image).float()\n\n        # Add an extra batch dimension since pytorch treats all images as batches\n        image_tensor = image_tensor.unsqueeze_(0).to(device)\n\n        batch_tensors.append(image_tensor)\n\n    # Concatenate all tensors in the batch\n    batch_tensors = torch.cat(batch_tensors)\n\n    # Perform inference\n    outputs = vgg16(batch_tensors)\n    _, predicted = torch.max(outputs, 1)\n\n    # Append to results\n    for i, image_file in enumerate(batch_files):\n        results.append([image_file, classes[predicted[i].item()]])\n\n    # Print time estimation\n    elapsed_time = time.time() - start_time\n    estimated_remaining_time = elapsed_time / (b + 1) * (num_batches - b - 1)\n    print(f\"Progress: {b+1}/{num_batches}, Estimated time left: {format_time(estimated_remaining_time)}\", end='\\r')\n\n\n# Save results to csv file\ndf = pd.DataFrame(results, columns=['id', 'label'])\ndf.to_csv('sampleSubmission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:02:28.853631Z","iopub.execute_input":"2023-07-12T16:02:28.854331Z","iopub.status.idle":"2023-07-12T16:30:30.501726Z","shell.execute_reply.started":"2023-07-12T16:02:28.854297Z","shell.execute_reply":"2023-07-12T16:30:30.500577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking if al 300,000 label is made\ncsv = pd.read_csv('sampleSubmission.csv')\nprint(csv.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:30:30.503881Z","iopub.execute_input":"2023-07-12T16:30:30.504186Z","iopub.status.idle":"2023-07-12T16:30:30.704191Z","shell.execute_reply.started":"2023-07-12T16:30:30.504159Z","shell.execute_reply":"2023-07-12T16:30:30.703156Z"},"trusted":true},"execution_count":null,"outputs":[]}]}