조현아

get 1 random policy

import os
import fire
import json
from pprint import pprint
import pickle
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
from utils import *
# command
# python getAugmented.py --model_path='logs/April_24_21:05:15__resnet50__None/'
def eval(model_path):
print('\n[+] Parse arguments')
kwargs_path = os.path.join(model_path, 'kwargs.json')
kwargs = json.loads(open(kwargs_path).read())
args, kwargs = parse_args(kwargs)
pprint(args)
device = torch.device('cuda' if args.use_cuda else 'cpu')
cp_path = os.path.join(model_path, 'augmentation.cp')
writer = SummaryWriter(log_dir=model_path)
print('\n[+] Load transform')
# list
with open(cp_path, 'rb') as f:
aug_transform_list = pickle.load(f)
augmented_image_list = [torch.Tensor(240,0)] * len(get_dataset(args, None, 'test'))
print('\n[+] Load dataset')
for aug_idx, aug_transform in enumerate(aug_transform_list):
dataset = get_dataset(args, aug_transform, 'test')
loader = iter(get_aug_dataloader(args, dataset))
for i, (images, target) in enumerate(loader):
images = images.view(240, 240)
# concat image
augmented_image_list[i] = torch.cat([augmented_image_list[i], images], dim = 1)
if i % 1000 == 0:
print("\n images size: ", augmented_image_list[i].size()) # [240, 240]
break
# break
# print(augmented_image_list)
print('\n[+] Write on tensorboard')
if writer:
for i, data in enumerate(augmented_image_list):
tag = 'img/' + str(i)
writer.add_image(tag, data.view(1, 240, -1), global_step=0)
break
writer.close()
# if writer:
# for j in range():
# tag = 'img/' + str(img_count) + '_' + str(j)
# # writer.add_image(tag,
# # concat_image_features(images[j], first[j]), global_step=step)
# # if j > 0:
# # fore = concat_image_features(fore, images[j])
# writer.add_image(tag, fore, global_step=0)
# img_count = img_count + 1
# writer.close()
if __name__ == '__main__':
fire.Fire(eval)
import os
import fire
import json
from pprint import pprint
import pickle
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
from utils import *
# command
# python getAugmented.py --model_path='logs/April_24_21:05:15__resnet50__None/'
def eval(model_path):
print('\n[+] Parse arguments')
kwargs_path = os.path.join(model_path, 'kwargs.json')
kwargs = json.loads(open(kwargs_path).read())
args, kwargs = parse_args(kwargs)
pprint(args)
device = torch.device('cuda' if args.use_cuda else 'cpu')
cp_path = os.path.join(model_path, 'augmentation.cp')
writer = SummaryWriter(log_dir=model_path)
print('\n[+] Load transform')
# list
with open(cp_path, 'rb') as f:
aug_transform_list = pickle.load(f)
augmented_image_list = [torch.Tensor(240,0)] * len(get_dataset(args, None, 'train'))
print('\n[+] Load dataset')
for aug_idx, aug_transform in enumerate(aug_transform_list):
dataset = get_dataset(args, aug_transform, 'train')
loader = iter(get_aug_dataloader(args, dataset))
for i, (images, target) in enumerate(loader):
images = images.view(240, 240)
# concat image
augmented_image_list[i] = torch.cat([augmented_image_list[i], images], dim = 1)
print('\n[+] Write on tensorboard')
if writer:
for i, data in enumerate(augmented_image_list):
tag = 'img/' + str(i)
writer.add_image(tag, data.view(1, 240, -1), global_step=0)
writer.close()
if __name__ == '__main__':
fire.Fire(eval)
import os
import fire
import json
from pprint import pprint
import pickle
import random
import torch
import torch.nn as nn
from torchvision.utils import save_image
from torch.utils.tensorboard import SummaryWriter
from utils import *
# command
# python getAugmented_saveimg.py --model_path='logs/April_26_00:55:16__resnet50__None/'
def eval(model_path):
print('\n[+] Parse arguments')
kwargs_path = os.path.join(model_path, 'kwargs.json')
kwargs = json.loads(open(kwargs_path).read())
args, kwargs = parse_args(kwargs)
pprint(args)
device = torch.device('cuda' if args.use_cuda else 'cpu')
cp_path = os.path.join(model_path, 'augmentation.cp')
writer = SummaryWriter(log_dir=model_path)
print('\n[+] Load transform')
# list to tensor
with open(cp_path, 'rb') as f:
aug_transform_list = pickle.load(f)
transform = transforms.RandomChoice(aug_transform_list)
print('\n[+] Load dataset')
dataset = get_dataset(args, transform, 'train')
loader = iter(get_aug_dataloader(args, dataset))
print('\n[+] Save 1 random policy')
os.makedirs(os.path.join(model_path, 'augmented_imgs'))
save_dir = os.path.join(model_path, 'augmented_imgs')
for i, (image, target) in enumerate(loader):
image = image.view(240, 240)
# save img
save_image(image, os.path.join(save_dir, 'aug_'+ str(i) + '.png'))
if(i % 100 == 0):
print("\n saved images: ", i)
print('\n[+] Finished to save')
if __name__ == '__main__':
fire.Fire(eval)
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "classify normal/lesion.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "AjoTMXMCrFYX",
"colab_type": "code",
"outputId": "2548434e-72b7-4946-9748-070103017379",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 129
}
},
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n",
"\n",
"Enter your authorization code:\n",
"··········\n",
"Mounted at /content/drive\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "lXK8NZfIyzeB",
"colab_type": "code",
"outputId": "17508683-94fb-45fa-b9df-f7ddb9401b68",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 146
}
},
"source": [
"!git clone http://khuhub.khu.ac.kr/2020-1-capstone-design2/2016104167.git"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"Cloning into '2016104167'...\n",
"remote: Counting objects: 11451, done.\u001b[K\n",
"remote: Compressing objects: 100% (39/39), done.\u001b[K\n",
"remote: Total 11451 (delta 15), reused 0 (delta 0)\u001b[K\n",
"Receiving objects: 100% (11451/11451), 292.82 MiB | 384.00 KiB/s, done.\n",
"Resolving deltas: 100% (1109/1109), done.\n",
"Checking out files: 100% (15684/15684), done.\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "TmGc36H2y5sI",
"colab_type": "code",
"outputId": "49ce70f0-10bb-48d8-bd89-de98a28c7893",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"%cd '2016104167/code/classifier/'"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"/content/2016104167/code/classifier\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "oJ08JUJCzEEE",
"colab_type": "code",
"outputId": "f9454032-73ad-444e-b708-e1228f6b3a0b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
}
},
"source": [
"!python -m pip install -r \"requirements.txt\""
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from -r requirements.txt (line 1)) (0.16.0)\n",
"Collecting tb-nightly\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/4e/46/4b95936aed44f2154d936160de6c58e3bd4cf8501152db21945617c84694/tb_nightly-2.3.0a20200425-py3-none-any.whl (2.9MB)\n",
"\u001b[K |████████████████████████████████| 2.9MB 39.9MB/s \n",
"\u001b[?25hRequirement already satisfied: hyperopt in /usr/local/lib/python3.6/dist-packages (from -r requirements.txt (line 3)) (0.1.2)\n",
"Collecting pillow==6.2.1\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/10/5c/0e94e689de2476c4c5e644a3bd223a1c1b9e2bdb7c510191750be74fa786/Pillow-6.2.1-cp36-cp36m-manylinux1_x86_64.whl (2.1MB)\n",
"\u001b[K |████████████████████████████████| 2.1MB 48.1MB/s \n",
"\u001b[?25hRequirement already satisfied: natsort in /usr/local/lib/python3.6/dist-packages (from -r requirements.txt (line 5)) (5.5.0)\n",
"Collecting fire\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/34/a7/0e22e70778aca01a52b9c899d9c145c6396d7b613719cd63db97ffa13f2f/fire-0.3.1.tar.gz (81kB)\n",
"\u001b[K |████████████████████████████████| 81kB 12.1MB/s \n",
"\u001b[?25hCollecting torchvision==0.2.2\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/ce/a1/66d72a2fe580a9f0fcbaaa5b976911fbbde9dce9b330ba12791997b856e9/torchvision-0.2.2-py2.py3-none-any.whl (64kB)\n",
"\u001b[K |████████████████████████████████| 71kB 11.8MB/s \n",
"\u001b[?25hCollecting torch==1.1.0\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/69/60/f685fb2cfb3088736bafbc9bdbb455327bdc8906b606da9c9a81bae1c81e/torch-1.1.0-cp36-cp36m-manylinux1_x86_64.whl (676.9MB)\n",
"\u001b[K |████████████████████████████████| 676.9MB 18kB/s \n",
"\u001b[?25hRequirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from -r requirements.txt (line 9)) (1.0.3)\n",
"Requirement already satisfied: sklearn in /usr/local/lib/python3.6/dist-packages (from -r requirements.txt (line 10)) (0.0)\n",
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (3.2.1)\n",
"Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (3.10.0)\n",
"Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (1.7.2)\n",
"Requirement already satisfied: numpy>=1.12.0 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (1.18.3)\n",
"Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (1.6.0.post3)\n",
"Requirement already satisfied: wheel>=0.26; python_version >= \"3\" in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (0.34.2)\n",
"Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (0.9.0)\n",
"Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (46.1.3)\n",
"Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (2.21.0)\n",
"Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (1.12.0)\n",
"Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (1.0.1)\n",
"Requirement already satisfied: grpcio>=1.24.3 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (1.28.1)\n",
"Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from tb-nightly->-r requirements.txt (line 2)) (0.4.1)\n",
"Requirement already satisfied: networkx in /usr/local/lib/python3.6/dist-packages (from hyperopt->-r requirements.txt (line 3)) (2.4)\n",
"Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from hyperopt->-r requirements.txt (line 3)) (4.38.0)\n",
"Requirement already satisfied: pymongo in /usr/local/lib/python3.6/dist-packages (from hyperopt->-r requirements.txt (line 3)) (3.10.1)\n",
"Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from hyperopt->-r requirements.txt (line 3)) (1.4.1)\n",
"Requirement already satisfied: termcolor in /usr/local/lib/python3.6/dist-packages (from fire->-r requirements.txt (line 6)) (1.1.0)\n",
"Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas->-r requirements.txt (line 9)) (2.8.1)\n",
"Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->-r requirements.txt (line 9)) (2018.9)\n",
"Requirement already satisfied: scikit-learn in /usr/local/lib/python3.6/dist-packages (from sklearn->-r requirements.txt (line 10)) (0.22.2.post1)\n",
"Requirement already satisfied: rsa<4.1,>=3.1.4 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tb-nightly->-r requirements.txt (line 2)) (4.0)\n",
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tb-nightly->-r requirements.txt (line 2)) (0.2.8)\n",
"Requirement already satisfied: cachetools<3.2,>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tb-nightly->-r requirements.txt (line 2)) (3.1.1)\n",
"Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tb-nightly->-r requirements.txt (line 2)) (3.0.4)\n",
"Requirement already satisfied: idna<2.9,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tb-nightly->-r requirements.txt (line 2)) (2.8)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tb-nightly->-r requirements.txt (line 2)) (2020.4.5.1)\n",
"Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tb-nightly->-r requirements.txt (line 2)) (1.24.3)\n",
"Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tb-nightly->-r requirements.txt (line 2)) (1.3.0)\n",
"Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.6/dist-packages (from networkx->hyperopt->-r requirements.txt (line 3)) (4.4.2)\n",
"Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.6/dist-packages (from scikit-learn->sklearn->-r requirements.txt (line 10)) (0.14.1)\n",
"Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.6/dist-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tb-nightly->-r requirements.txt (line 2)) (0.4.8)\n",
"Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tb-nightly->-r requirements.txt (line 2)) (3.1.0)\n",
"Building wheels for collected packages: fire\n",
" Building wheel for fire (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Created wheel for fire: filename=fire-0.3.1-py2.py3-none-any.whl size=111005 sha256=090cc0b99c44969c5594966fd1af925b4ff73b02719d44b05fb4aacd07b9bfb3\n",
" Stored in directory: /root/.cache/pip/wheels/c1/61/df/768b03527bf006b546dce284eb4249b185669e65afc5fbb2ac\n",
"Successfully built fire\n",
"\u001b[31mERROR: torchvision 0.2.2 has requirement tqdm==4.19.9, but you'll have tqdm 4.38.0 which is incompatible.\u001b[0m\n",
"\u001b[31mERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.\u001b[0m\n",
"Installing collected packages: tb-nightly, pillow, fire, torch, torchvision\n",
" Found existing installation: Pillow 7.0.0\n",
" Uninstalling Pillow-7.0.0:\n",
" Successfully uninstalled Pillow-7.0.0\n",
" Found existing installation: torch 1.4.0\n",
" Uninstalling torch-1.4.0:\n",
" Successfully uninstalled torch-1.4.0\n",
" Found existing installation: torchvision 0.5.0\n",
" Uninstalling torchvision-0.5.0:\n",
" Successfully uninstalled torchvision-0.5.0\n",
"Successfully installed fire-0.3.1 pillow-6.2.1 tb-nightly-2.3.0a20200425 torch-1.1.0 torchvision-0.2.2\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "jdayOoSYHJDf",
"colab_type": "code",
"outputId": "f663313d-1166-4f6c-f617-d3d331ce7793",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
}
},
"source": [
"!python train.py --use_cuda=True --network=resnet50 --optimizer=adam "
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"[+] Parse arguments\n",
"Args(augment_path=None, batch_size=8, dataset='BraTS', learning_rate=1e-06, max_step=2000, network='resnet50', num_workers=4, optimizer='adam', print_step=50, scheduler='exp', seed=None, start_step=0, use_cuda=True, val_step=50)\n",
"\n",
"[+] Create log dir\n",
"2020-04-26 04:35:05.510266: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1\n",
"\n",
"[+] Create network\n",
"\n",
"[+] Load dataset\n",
"\n",
"[+] Start training\n",
"\n",
"[+] Use 1 GPUs\n",
"\n",
"[+] Using GPU: Tesla P4 \n",
"\n",
"[+] Training step: 0/2000\tTraining epoch: 0/580\tElapsed time: 0.01min\tLearning rate: 9.999283e-07\n",
" Acc@1 : 0.000%\n",
" Loss : 7.1226911544799805\n",
" FW Time : 125.206ms\n",
" BW Time : 14.758ms\n",
"\n",
"[+] (Valid results) Valid step: 49/2000\n",
" Acc@1 : 0.000%\n",
" Loss : 7.333343982696533\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 50/2000\tTraining epoch: 0/580\tElapsed time: 0.17min\tLearning rate: 9.963498469652168e-07\n",
" Acc@1 : 0.000%\n",
" Loss : 6.839444160461426\n",
" FW Time : 15.798ms\n",
" BW Time : 10.145ms\n",
"\n",
"[+] (Valid results) Valid step: 99/2000\n",
" Acc@1 : 0.000%\n",
" Loss : 7.0915045738220215\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 100/2000\tTraining epoch: 0/580\tElapsed time: 0.33min\tLearning rate: 9.92784200174764e-07\n",
" Acc@1 : 0.000%\n",
" Loss : 6.130648136138916\n",
" FW Time : 15.954ms\n",
" BW Time : 13.310ms\n",
"\n",
"[+] (Valid results) Valid step: 149/2000\n",
" Acc@1 : 12.000%\n",
" Loss : 6.664271354675293\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 150/2000\tTraining epoch: 0/580\tElapsed time: 0.49min\tLearning rate: 9.89231313798811e-07\n",
" Acc@1 : 25.000%\n",
" Loss : 5.820688247680664\n",
" FW Time : 15.717ms\n",
" BW Time : 12.132ms\n",
"\n",
"[+] (Valid results) Valid step: 199/2000\n",
" Acc@1 : 38.400%\n",
" Loss : 6.647609233856201\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 200/2000\tTraining epoch: 0/580\tElapsed time: 0.65min\tLearning rate: 9.85691142171539e-07\n",
" Acc@1 : 62.500%\n",
" Loss : 5.464842319488525\n",
" FW Time : 18.873ms\n",
" BW Time : 14.734ms\n",
"\n",
"[+] (Valid results) Valid step: 249/2000\n",
" Acc@1 : 51.600%\n",
" Loss : 6.3311767578125\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 250/2000\tTraining epoch: 0/580\tElapsed time: 0.80min\tLearning rate: 9.821636397905564e-07\n",
" Acc@1 : 62.500%\n",
" Loss : 5.102325439453125\n",
" FW Time : 20.299ms\n",
" BW Time : 13.266ms\n",
"\n",
"[+] (Valid results) Valid step: 299/2000\n",
" Acc@1 : 65.000%\n",
" Loss : 5.964324951171875\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 300/2000\tTraining epoch: 0/580\tElapsed time: 0.97min\tLearning rate: 9.786487613163075e-07\n",
" Acc@1 : 75.000%\n",
" Loss : 4.567136287689209\n",
" FW Time : 22.482ms\n",
" BW Time : 11.849ms\n",
"\n",
"[+] (Valid results) Valid step: 349/2000\n",
" Acc@1 : 67.000%\n",
" Loss : 5.935492038726807\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 350/2000\tTraining epoch: 0/580\tElapsed time: 1.13min\tLearning rate: 9.751464615714972e-07\n",
" Acc@1 : 75.000%\n",
" Loss : 4.528119087219238\n",
" FW Time : 15.663ms\n",
" BW Time : 12.312ms\n",
"\n",
"[+] (Valid results) Valid step: 399/2000\n",
" Acc@1 : 66.200%\n",
" Loss : 6.177009105682373\n",
"\n",
"[+] Training step: 400/2000\tTraining epoch: 0/580\tElapsed time: 1.28min\tLearning rate: 9.716566955405052e-07\n",
" Acc@1 : 50.000%\n",
" Loss : 5.060157775878906\n",
" FW Time : 18.538ms\n",
" BW Time : 15.068ms\n",
"\n",
"[+] (Valid results) Valid step: 449/2000\n",
" Acc@1 : 70.400%\n",
" Loss : 5.513169288635254\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 450/2000\tTraining epoch: 0/580\tElapsed time: 1.44min\tLearning rate: 9.681794183688074e-07\n",
" Acc@1 : 62.500%\n",
" Loss : 4.341537952423096\n",
" FW Time : 20.281ms\n",
" BW Time : 11.464ms\n",
"\n",
"[+] (Valid results) Valid step: 499/2000\n",
" Acc@1 : 71.200%\n",
" Loss : 5.211921691894531\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 500/2000\tTraining epoch: 0/580\tElapsed time: 1.60min\tLearning rate: 9.647145853624042e-07\n",
" Acc@1 : 62.500%\n",
" Loss : 3.643502712249756\n",
" FW Time : 35.242ms\n",
" BW Time : 13.355ms\n",
"\n",
"[+] (Valid results) Valid step: 549/2000\n",
" Acc@1 : 73.000%\n",
" Loss : 5.065425872802734\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 550/2000\tTraining epoch: 0/580\tElapsed time: 1.76min\tLearning rate: 9.61262151987242e-07\n",
" Acc@1 : 37.500%\n",
" Loss : 4.352468013763428\n",
" FW Time : 15.172ms\n",
" BW Time : 14.894ms\n",
"\n",
"[+] (Valid results) Valid step: 599/2000\n",
" Acc@1 : 72.800%\n",
" Loss : 5.094676971435547\n",
"\n",
"[+] Training step: 600/2000\tTraining epoch: 0/580\tElapsed time: 1.92min\tLearning rate: 9.578220738686398e-07\n",
" Acc@1 : 62.500%\n",
" Loss : 2.67336106300354\n",
" FW Time : 16.094ms\n",
" BW Time : 11.950ms\n",
"\n",
"[+] (Valid results) Valid step: 649/2000\n",
" Acc@1 : 74.200%\n",
" Loss : 4.8995866775512695\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 650/2000\tTraining epoch: 0/580\tElapsed time: 2.08min\tLearning rate: 9.543943067907226e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 2.3277320861816406\n",
" FW Time : 22.967ms\n",
" BW Time : 16.513ms\n",
"\n",
"[+] (Valid results) Valid step: 699/2000\n",
" Acc@1 : 78.000%\n",
" Loss : 4.331630706787109\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 700/2000\tTraining epoch: 0/580\tElapsed time: 2.24min\tLearning rate: 9.509788066958503e-07\n",
" Acc@1 : 50.000%\n",
" Loss : 4.2444071769714355\n",
" FW Time : 26.178ms\n",
" BW Time : 20.318ms\n",
"\n",
"[+] (Valid results) Valid step: 749/2000\n",
" Acc@1 : 79.800%\n",
" Loss : 4.202180862426758\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 750/2000\tTraining epoch: 0/580\tElapsed time: 2.40min\tLearning rate: 9.475755296840536e-07\n",
" Acc@1 : 75.000%\n",
" Loss : 2.0681729316711426\n",
" FW Time : 14.539ms\n",
" BW Time : 9.137ms\n",
"\n",
"[+] (Valid results) Valid step: 799/2000\n",
" Acc@1 : 81.800%\n",
" Loss : 3.595407009124756\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 800/2000\tTraining epoch: 0/580\tElapsed time: 2.56min\tLearning rate: 9.441844320124666e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 1.7526265382766724\n",
" FW Time : 16.346ms\n",
" BW Time : 13.979ms\n",
"\n",
"[+] (Valid results) Valid step: 849/2000\n",
" Acc@1 : 82.800%\n",
" Loss : 3.435865879058838\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 850/2000\tTraining epoch: 0/580\tElapsed time: 2.72min\tLearning rate: 9.408054700947673e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 1.327768325805664\n",
" FW Time : 20.280ms\n",
" BW Time : 11.583ms\n",
"\n",
"[+] (Valid results) Valid step: 899/2000\n",
" Acc@1 : 86.800%\n",
" Loss : 3.0217819213867188\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 900/2000\tTraining epoch: 0/580\tElapsed time: 2.88min\tLearning rate: 9.37438600500616e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 1.7023265361785889\n",
" FW Time : 16.003ms\n",
" BW Time : 11.639ms\n",
"\n",
"[+] (Valid results) Valid step: 949/2000\n",
" Acc@1 : 89.600%\n",
" Loss : 2.5056638717651367\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 950/2000\tTraining epoch: 0/580\tElapsed time: 3.04min\tLearning rate: 9.340837799550989e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 2.0191428661346436\n",
" FW Time : 26.238ms\n",
" BW Time : 11.809ms\n",
"\n",
"[+] (Valid results) Valid step: 999/2000\n",
" Acc@1 : 92.600%\n",
" Loss : 1.8181736469268799\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1000/2000\tTraining epoch: 0/580\tElapsed time: 3.20min\tLearning rate: 9.307409653381686e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 0.9778107404708862\n",
" FW Time : 18.434ms\n",
" BW Time : 17.896ms\n",
"\n",
"[+] (Valid results) Valid step: 1049/2000\n",
" Acc@1 : 94.400%\n",
" Loss : 1.7727973461151123\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1050/2000\tTraining epoch: 0/580\tElapsed time: 3.36min\tLearning rate: 9.274101136840937e-07\n",
" Acc@1 : 75.000%\n",
" Loss : 1.6472196578979492\n",
" FW Time : 22.264ms\n",
" BW Time : 11.575ms\n",
"\n",
"[+] (Valid results) Valid step: 1099/2000\n",
" Acc@1 : 96.400%\n",
" Loss : 1.3225497007369995\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1100/2000\tTraining epoch: 0/580\tElapsed time: 3.52min\tLearning rate: 9.240911821809037e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 1.520838737487793\n",
" FW Time : 17.655ms\n",
" BW Time : 11.358ms\n",
"\n",
"[+] (Valid results) Valid step: 1149/2000\n",
" Acc@1 : 97.200%\n",
" Loss : 1.0447773933410645\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1150/2000\tTraining epoch: 0/580\tElapsed time: 3.69min\tLearning rate: 9.207841281698394e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.45810914039611816\n",
" FW Time : 21.189ms\n",
" BW Time : 21.055ms\n",
"\n",
"[+] (Valid results) Valid step: 1199/2000\n",
" Acc@1 : 97.200%\n",
" Loss : 0.6157697439193726\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1200/2000\tTraining epoch: 0/580\tElapsed time: 3.85min\tLearning rate: 9.174889091448058e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.2990947961807251\n",
" FW Time : 16.315ms\n",
" BW Time : 13.309ms\n",
"\n",
"[+] (Valid results) Valid step: 1249/2000\n",
" Acc@1 : 98.200%\n",
" Loss : 0.9130725860595703\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1250/2000\tTraining epoch: 0/580\tElapsed time: 4.01min\tLearning rate: 9.142054827518248e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.2261216640472412\n",
" FW Time : 30.053ms\n",
" BW Time : 18.239ms\n",
"\n",
"[+] (Valid results) Valid step: 1299/2000\n",
" Acc@1 : 98.000%\n",
" Loss : 0.4017503261566162\n",
"\n",
"[+] Training step: 1300/2000\tTraining epoch: 0/580\tElapsed time: 4.17min\tLearning rate: 9.109338067884897e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.3248733878135681\n",
" FW Time : 21.094ms\n",
" BW Time : 13.526ms\n",
"\n",
"[+] (Valid results) Valid step: 1349/2000\n",
" Acc@1 : 98.000%\n",
" Loss : 0.3138556480407715\n",
"\n",
"[+] Training step: 1350/2000\tTraining epoch: 0/580\tElapsed time: 4.32min\tLearning rate: 9.076738392034251e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.21796566247940063\n",
" FW Time : 25.850ms\n",
" BW Time : 19.265ms\n",
"\n",
"[+] (Valid results) Valid step: 1399/2000\n",
" Acc@1 : 98.000%\n",
" Loss : 0.16485238075256348\n",
"\n",
"[+] Training step: 1400/2000\tTraining epoch: 0/580\tElapsed time: 4.48min\tLearning rate: 9.044255380957452e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.28114575147628784\n",
" FW Time : 22.245ms\n",
" BW Time : 15.607ms\n",
"\n",
"[+] (Valid results) Valid step: 1449/2000\n",
" Acc@1 : 98.200%\n",
" Loss : 0.20785784721374512\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1450/2000\tTraining epoch: 0/580\tElapsed time: 4.64min\tLearning rate: 9.011888617145144e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 1.18643319606781\n",
" FW Time : 19.665ms\n",
" BW Time : 12.618ms\n",
"\n",
"[+] (Valid results) Valid step: 1499/2000\n",
" Acc@1 : 98.400%\n",
" Loss : 0.17331039905548096\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1500/2000\tTraining epoch: 0/580\tElapsed time: 4.80min\tLearning rate: 8.979637684582136e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.27240896224975586\n",
" FW Time : 24.560ms\n",
" BW Time : 14.096ms\n",
"\n",
"[+] (Valid results) Valid step: 1549/2000\n",
" Acc@1 : 99.600%\n",
" Loss : 0.17186295986175537\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1550/2000\tTraining epoch: 0/580\tElapsed time: 4.96min\tLearning rate: 8.947502168742003e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 0.6044453382492065\n",
" FW Time : 20.771ms\n",
" BW Time : 16.470ms\n",
"\n",
"[+] (Valid results) Valid step: 1599/2000\n",
" Acc@1 : 99.200%\n",
" Loss : 0.06833314895629883\n",
"\n",
"[+] Training step: 1600/2000\tTraining epoch: 0/580\tElapsed time: 5.12min\tLearning rate: 8.915481656581816e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.1431320309638977\n",
" FW Time : 20.891ms\n",
" BW Time : 12.692ms\n",
"\n",
"[+] (Valid results) Valid step: 1649/2000\n",
" Acc@1 : 99.600%\n",
" Loss : 0.09469139575958252\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1650/2000\tTraining epoch: 0/580\tElapsed time: 5.28min\tLearning rate: 8.8835757365368e-07\n",
" Acc@1 : 87.500%\n",
" Loss : 0.8603015542030334\n",
" FW Time : 15.960ms\n",
" BW Time : 16.599ms\n",
"\n",
"[+] (Valid results) Valid step: 1699/2000\n",
" Acc@1 : 99.600%\n",
" Loss : 0.061615824699401855\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1700/2000\tTraining epoch: 0/580\tElapsed time: 5.44min\tLearning rate: 8.851783998515047e-07\n",
" Acc@1 : 62.500%\n",
" Loss : 1.4177227020263672\n",
" FW Time : 14.728ms\n",
" BW Time : 14.786ms\n",
"\n",
"[+] (Valid results) Valid step: 1749/2000\n",
" Acc@1 : 99.600%\n",
" Loss : 0.03962230682373047\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1750/2000\tTraining epoch: 0/580\tElapsed time: 5.61min\tLearning rate: 8.820106033892254e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.13233846426010132\n",
" FW Time : 15.973ms\n",
" BW Time : 13.815ms\n",
"\n",
"[+] (Valid results) Valid step: 1799/2000\n",
" Acc@1 : 99.800%\n",
" Loss : 0.044447898864746094\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1800/2000\tTraining epoch: 0/580\tElapsed time: 5.77min\tLearning rate: 8.788541435506462e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.08350610733032227\n",
" FW Time : 18.745ms\n",
" BW Time : 13.062ms\n",
"\n",
"[+] (Valid results) Valid step: 1849/2000\n",
" Acc@1 : 100.000%\n",
" Loss : 0.06921112537384033\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1850/2000\tTraining epoch: 0/580\tElapsed time: 5.93min\tLearning rate: 8.757089797652821e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.14804929494857788\n",
" FW Time : 41.547ms\n",
" BW Time : 15.775ms\n",
"\n",
"[+] (Valid results) Valid step: 1899/2000\n",
" Acc@1 : 100.000%\n",
" Loss : 0.06944191455841064\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1900/2000\tTraining epoch: 0/580\tElapsed time: 6.09min\tLearning rate: 8.725750716078392e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.027304232120513916\n",
" FW Time : 21.632ms\n",
" BW Time : 17.365ms\n",
"\n",
"[+] (Valid results) Valid step: 1949/2000\n",
" Acc@1 : 100.000%\n",
" Loss : 0.05875754356384277\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 1950/2000\tTraining epoch: 0/580\tElapsed time: 6.25min\tLearning rate: 8.694523787976934e-07\n",
" Acc@1 : 100.000%\n",
" Loss : 0.13626277446746826\n",
" FW Time : 40.104ms\n",
" BW Time : 14.362ms\n",
"\n",
"[+] (Valid results) Valid step: 1999/2000\n",
" Acc@1 : 100.000%\n",
" Loss : 0.0167849063873291\n",
"\n",
"[+] Model saved\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "3opAMwCLZYJC",
"colab_type": "code",
"outputId": "208d8102-ce39-4897-9f89-ed7f8f626c33",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 350
}
},
"source": [
"!python \"eval.py\" --model_path='/content/drive/My Drive/CD2 Project/runs/classify/April_26_13:35:05__resnet50__None/' "
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"[+] Parse arguments\n",
"Args(augment_path=None, batch_size=8, dataset='BraTS', learning_rate=1e-06, max_step=2000, network='resnet50', num_workers=4, optimizer='adam', print_step=50, scheduler='exp', seed=None, start_step=0, use_cuda=True, val_step=50)\n",
"\n",
"[+] Create network\n",
"\n",
"[+] Load model\n",
"\n",
"[+] Load dataset\n",
"\n",
"[+] Start testing\n",
"2020-04-26 05:02:26.970817: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1\n",
"\n",
"[+] Valid results\n",
" Acc@1 : 100.000%\n",
" Loss : 0.020\n",
" Infer Time(per image) : 4.062ms\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "F70Y3J9DHJwy",
"colab_type": "code",
"outputId": "502a62a8-de44-42c8-ec77-afc838a068c8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
}
},
"source": [
"# train cifar10\n",
"# resnet50 from pytorch\n",
"# !python train.py --dataset=cifar10 --use_cuda=True --optimizer=adam --network=resnet50"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"[+] Parse arguments\n",
"Args(augment_path=None, batch_size=32, dataset='cifar10', learning_rate=0.001, max_step=500, network='resnet50', num_workers=4, optimizer='adam', print_step=100, scheduler='exp', seed=None, start_step=0, use_cuda=True, val_step=100)\n",
"\n",
"[+] Create log dir\n",
"\n",
"[+] Create network\n",
"\n",
"[+] Load dataset\n",
"Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to /content/drive/My Drive/CD2 Project/cifar-10-python.tar.gz\n",
"171MB [00:02, 70.7MB/s] \n",
"Files already downloaded and verified\n",
"\n",
"[+] Start training\n",
"\n",
"[+] Use 1 GPUs\n",
"\n",
"[+] Using GPU: Tesla P100-PCIE-16GB \n",
"\n",
"[+] Training step: 0/500\tTraining epoch: 0/1406\tElapsed time: 0.01min\tLearning rate: 0.0009999283\n",
" Acc@1 : 0.000%\n",
" Loss : 6.797711372375488\n",
" FW Time : 63.167ms\n",
" BW Time : 14.364ms\n",
"\n",
"[+] Valid results\n",
" Acc@1 : 22.620%\n",
" Loss : 2.110281229019165\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 100/500\tTraining epoch: 0/1406\tElapsed time: 0.20min\tLearning rate: 0.000992784200174764\n",
" Acc@1 : 12.500%\n",
" Loss : 2.331458568572998\n",
" FW Time : 28.192ms\n",
" BW Time : 17.216ms\n",
"\n",
"[+] Valid results\n",
" Acc@1 : 27.320%\n",
" Loss : 1.5727815628051758\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 200/500\tTraining epoch: 0/1406\tElapsed time: 0.36min\tLearning rate: 0.0009856911421715388\n",
" Acc@1 : 25.000%\n",
" Loss : 1.8888376951217651\n",
" FW Time : 26.493ms\n",
" BW Time : 17.836ms\n",
"\n",
"[+] Valid results\n",
" Acc@1 : 30.800%\n",
" Loss : 1.4916565418243408\n",
"\n",
"[+] Model saved\n",
"\n",
"[+] Training step: 300/500\tTraining epoch: 0/1406\tElapsed time: 0.53min\tLearning rate: 0.0009786487613163062\n",
" Acc@1 : 25.000%\n",
" Loss : 2.3930575847625732\n",
" FW Time : 24.473ms\n",
" BW Time : 22.187ms\n",
"\n",
"[+] Valid results\n",
" Acc@1 : 24.680%\n",
" Loss : 1.992270827293396\n",
"\n",
"[+] Training step: 400/500\tTraining epoch: 0/1406\tElapsed time: 0.70min\tLearning rate: 0.000971656695540503\n",
" Acc@1 : 25.000%\n",
" Loss : 2.1769983768463135\n",
" FW Time : 28.696ms\n",
" BW Time : 22.384ms\n",
"\n",
"[+] Valid results\n",
" Acc@1 : 32.320%\n",
" Loss : 1.5474934577941895\n",
"\n",
"[+] Model saved\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Mhw6fBwCpHRd",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
\ No newline at end of file
import os
import time
import importlib
import collections
import pickle as cp
import glob
import numpy as np
import pandas as pd
from natsort import natsorted
from PIL import Image
import torch
import torchvision
import torch.nn.functional as F
import torchvision.models as models
import torchvision.transforms as transforms
from torch.utils.data import Subset
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from networks import *
TRAIN_DATASET_PATH = '/content/drive/My Drive/CD2 Project/data/nonaug+Normal_train/'
TRAIN_TARGET_PATH = '/content/drive/My Drive/CD2 Project/data/train_nonaug_classify_target.csv'
# VAL_DATASET_PATH = '../../data/MICCAI_BraTS_2019_Data_Training/ce_valid/'
# VAL_TARGET_PATH = '../../data/MICCAI_BraTS_2019_Data_Training/ce_valid_targets.csv'
current_epoch = 0
def split_dataset(args, dataset, k):
# load dataset
X = list(range(len(dataset)))
Y = dataset.targets
# split to k-fold
assert len(X) == len(Y)
def _it_to_list(_it):
return list(zip(*list(_it)))
sss = StratifiedShuffleSplit(n_splits=k, random_state=args.seed, test_size=0.1)
Dm_indexes, Da_indexes = _it_to_list(sss.split(X, Y))
return Dm_indexes, Da_indexes
def get_model_name(args):
from datetime import datetime, timedelta, timezone
now = datetime.now(timezone.utc)
tz = timezone(timedelta(hours=9))
now = now.astimezone(tz)
date_time = now.strftime("%B_%d_%H:%M:%S")
model_name = '__'.join([date_time, args.network, str(args.seed)])
return model_name
def dict_to_namedtuple(d):
Args = collections.namedtuple('Args', sorted(d.keys()))
for k,v in d.items():
if type(v) is dict:
d[k] = dict_to_namedtuple(v)
elif type(v) is str:
try:
d[k] = eval(v)
except:
d[k] = v
args = Args(**d)
return args
def parse_args(kwargs):
# combine with default args
kwargs['dataset'] = kwargs['dataset'] if 'dataset' in kwargs else 'BraTS'
kwargs['network'] = kwargs['network'] if 'network' in kwargs else 'resnet50'
kwargs['optimizer'] = kwargs['optimizer'] if 'optimizer' in kwargs else 'adam'
kwargs['learning_rate'] = kwargs['learning_rate'] if 'learning_rate' in kwargs else 0.0001
kwargs['seed'] = kwargs['seed'] if 'seed' in kwargs else None
kwargs['use_cuda'] = kwargs['use_cuda'] if 'use_cuda' in kwargs else True
kwargs['use_cuda'] = kwargs['use_cuda'] and torch.cuda.is_available()
kwargs['num_workers'] = kwargs['num_workers'] if 'num_workers' in kwargs else 4
kwargs['print_step'] = kwargs['print_step'] if 'print_step' in kwargs else 500
kwargs['val_step'] = kwargs['val_step'] if 'val_step' in kwargs else 500
kwargs['scheduler'] = kwargs['scheduler'] if 'scheduler' in kwargs else 'exp'
kwargs['batch_size'] = kwargs['batch_size'] if 'batch_size' in kwargs else 128
kwargs['start_step'] = kwargs['start_step'] if 'start_step' in kwargs else 0
kwargs['max_step'] = kwargs['max_step'] if 'max_step' in kwargs else 5000
kwargs['augment_path'] = kwargs['augment_path'] if 'augment_path' in kwargs else None
# to named tuple
args = dict_to_namedtuple(kwargs)
return args, kwargs
def select_model(args):
# grayResNet2
resnet_dict = {'resnet18':grayResNet2.resnet18(), 'resnet34':grayResNet2.resnet34(),
'resnet50':grayResNet2.resnet50(), 'resnet101':grayResNet2.resnet101(), 'resnet152':grayResNet2.resnet152()}
if args.network in resnet_dict:
backbone = resnet_dict[args.network]
model = basenet.BaseNet(backbone, args)
else:
Net = getattr(importlib.import_module('networks.{}'.format(args.network)), 'Net')
model = Net(args)
#print(model) # print model architecture
return model
def select_optimizer(args, model):
if args.optimizer == 'sgd':
optimizer = torch.optim.SGD(model.parameters(), lr=args.learning_rate, momentum=0.9, weight_decay=0.0001)
elif args.optimizer == 'rms':
optimizer = torch.optim.RMSprop(model.parameters(), lr=args.learning_rate)
elif args.optimizer == 'adam':
optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate)
else:
raise Exception('Unknown Optimizer')
return optimizer
def select_scheduler(args, optimizer):
if not args.scheduler or args.scheduler == 'None':
return None
elif args.scheduler =='clr':
return torch.optim.lr_scheduler.CyclicLR(optimizer, 0.01, 0.015, mode='triangular2', step_size_up=250000, cycle_momentum=False)
elif args.scheduler =='exp':
return torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.9999283, last_epoch=-1)
else:
raise Exception('Unknown Scheduler')
class CustomDataset(Dataset):
def __init__(self, data_path, csv_path):
self.len = len(self.imgs)
self.path = data_path
self.imgs = natsorted(os.listdir(data_path))
df = pd.read_csv(csv_path)
targets_list = []
for fname in self.imgs:
row = df.loc[df['filename'] == fname]
targets_list.append(row.iloc[0, 1])
self.targets = targets_list
def __len__(self):
return self.len
def __getitem__(self, idx):
img_loc = os.path.join(self.path, self.imgs[idx])
targets = self.targets[idx]
image = Image.open(img_loc)
return image, targets
def get_dataset(args, transform, split='train'):
assert split in ['train', 'val', 'test', 'trainval']
if split in ['train']:
dataset = CustomDataset(TRAIN_DATASET_PATH, TRAIN_TARGET_PATH, transform=transform)
else: #test
dataset = CustomDataset(VAL_DATASET_PATH, VAL_TARGET_PATH, transform=transform)
return dataset
def get_dataloader(args, dataset, shuffle=False, pin_memory=True):
data_loader = torch.utils.data.DataLoader(dataset,
batch_size=args.batch_size,
shuffle=shuffle,
num_workers=args.num_workers,
pin_memory=pin_memory)
return data_loader
def get_aug_dataloader(args, dataset, shuffle=False, pin_memory=True):
data_loader = torch.utils.data.DataLoader(dataset,
batch_size=args.batch_size,
shuffle=shuffle,
num_workers=args.num_workers,
pin_memory=pin_memory)
return data_loader
def get_inf_dataloader(args, dataset):
global current_epoch
data_loader = iter(get_dataloader(args, dataset, shuffle=True))
while True:
try:
batch = next(data_loader)
except StopIteration:
current_epoch += 1
data_loader = iter(get_dataloader(args, dataset, shuffle=True))
batch = next(data_loader)
yield batch
def train_step(args, model, optimizer, scheduler, criterion, batch, step, writer, device=None):
model.train()
images, target = batch
if device:
images = images.to(device)
target = target.to(device)
elif args.use_cuda:
images = images.cuda(non_blocking=True)
target = target.cuda(non_blocking=True)
# compute output
start_t = time.time()
output, first = model(images)
forward_t = time.time() - start_t
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
acc1 /= images.size(0)
acc5 /= images.size(0)
# compute gradient and do SGD step
optimizer.zero_grad()
start_t = time.time()
loss.backward()
backward_t = time.time() - start_t
optimizer.step()
if scheduler: scheduler.step()
if writer and step % args.print_step == 0:
n_imgs = min(images.size(0), 10)
tag = 'train/' + str(step)
for j in range(n_imgs):
writer.add_image(tag,
concat_image_features(images[j], first[j]), global_step=step)
return acc1, acc5, loss, forward_t, backward_t
#_acc1, _acc5 = accuracy(output, target, topk=(1, 5))
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k)
return res