{"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":"code","source":"%matplotlib inline\nimport matplotlib\nimport matplotlib.pyplot as plt\nfrom IPython import display","metadata":{"execution":{"iopub.status.busy":"2022-06-09T10:40:11.296862Z","iopub.execute_input":"2022-06-09T10:40:11.297945Z","iopub.status.idle":"2022-06-09T10:40:11.326259Z","shell.execute_reply.started":"2022-06-09T10:40:11.297861Z","shell.execute_reply":"2022-06-09T10:40:11.325562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip  uninstall tridentx -y \n!pip install ../input/trident/tridentx-0.7.5-py3-none-any.whl --upgrade\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-09T10:40:13.270404Z","iopub.execute_input":"2022-06-09T10:40:13.271150Z","iopub.status.idle":"2022-06-09T10:40:26.092795Z","shell.execute_reply.started":"2022-06-09T10:40:13.271113Z","shell.execute_reply":"2022-06-09T10:40:26.091679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['TRIDENT_BACKEND']='pytorch'\nimport trident as T\nfrom trident import *","metadata":{"execution":{"iopub.status.busy":"2022-06-09T10:40:26.094565Z","iopub.execute_input":"2022-06-09T10:40:26.094934Z","iopub.status.idle":"2022-06-09T10:40:32.978332Z","shell.execute_reply.started":"2022-06-09T10:40:26.094893Z","shell.execute_reply":"2022-06-09T10:40:32.977473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf=pd.read_csv('../input/sorghum-id-fgvc-9/train_cultivar_mapping.csv').dropna()\ndf","metadata":{"execution":{"iopub.status.busy":"2022-06-09T10:40:40.579454Z","iopub.execute_input":"2022-06-09T10:40:40.580389Z","iopub.status.idle":"2022-06-09T10:40:40.619703Z","shell.execute_reply.started":"2022-06-09T10:40:40.580339Z","shell.execute_reply":"2022-06-09T10:40:40.618936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classnames=df.cultivar.unique().tolist()\nclassnames=list(sorted(classnames))\nprint(classnames)","metadata":{"execution":{"iopub.status.busy":"2022-06-09T10:44:28.594747Z","iopub.execute_input":"2022-06-09T10:44:28.595233Z","iopub.status.idle":"2022-06-09T10:44:28.607093Z","shell.execute_reply.started":"2022-06-09T10:44:28.595197Z","shell.execute_reply":"2022-06-09T10:44:28.606294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nall_images=glob.glob('../input/sorghum-id-fgvc-9/train_images/*.*g')\nprint(len(all_images))\n\nimages=[]\nlabels=[]\n\nfor index, row in df.iterrows():\n    impath='../input/sorghum-id-fgvc-9/train_images/'+row['image']\n    if impath in all_images:\n        images.append(impath)\n        labels.append(classnames.index(row['cultivar']))\n        \nprint(len(images))\nprint(len(labels))","metadata":{"execution":{"iopub.status.busy":"2022-06-09T10:47:07.536582Z","iopub.execute_input":"2022-06-09T10:47:07.537576Z","iopub.status.idle":"2022-06-09T10:47:15.237928Z","shell.execute_reply.started":"2022-06-09T10:47:07.537526Z","shell.execute_reply":"2022-06-09T10:47:15.237107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds1=ImageDataset(images,symbol='images')\nds2=LabelDataset(labels,symbol='labels')\ndata_provider=DataProvider(traindata=Iterator(data=ds1,label=ds2))\n\ndata_provider.image_transform_funcs = [\n    RandomTransform(rotation_range=45, zoom_range=(0.9,1.2), shift_range=0.05, shear_range=0.1, random_flip=0.2,keep_prob=0.3,border_mode='zero'), \n    RandomAdjustGamma(gamma_range=(0.6,1.1)),\n    RandomAdjustSaturation(value_range=(0.8, 1.6)),\n    RandomAdjustContrast(value_range=(0.8, 1.4)),\n    AutoLevel(),\n    SaltPepperNoise(prob=0.002),  # 椒鹽噪音\n    Normalize(127.5, 127.5)]","metadata":{"execution":{"iopub.status.busy":"2022-06-09T10:54:56.481530Z","iopub.execute_input":"2022-06-09T10:54:56.482094Z","iopub.status.idle":"2022-06-09T10:54:59.314279Z","shell.execute_reply.started":"2022-06-09T10:54:56.482059Z","shell.execute_reply":"2022-06-09T10:54:59.313473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_provider.preview_images()","metadata":{"execution":{"iopub.status.busy":"2022-06-09T10:55:02.857326Z","iopub.execute_input":"2022-06-09T10:55:02.857696Z","iopub.status.idle":"2022-06-09T10:55:07.657366Z","shell.execute_reply.started":"2022-06-09T10:55:02.857665Z","shell.execute_reply":"2022-06-09T10:55:07.656459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from trident.models import efficientnet\n\neffb0=efficientnet.EfficientNetB0(pretrained=True,classes=100)\neffb0.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-06-09T10:57:03.312948Z","iopub.execute_input":"2022-06-09T10:57:03.313741Z","iopub.status.idle":"2022-06-09T10:57:13.511993Z","shell.execute_reply.started":"2022-06-09T10:57:03.313708Z","shell.execute_reply":"2022-06-09T10:57:13.511205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\neffb0.with_optimizer(optimizer=DiffGrad, lr=1e-3)\\\n    .with_loss(CrossEntropyLoss) \\\n    .with_metric(accuracy, name='accuracy')\\\n    .with_metric(accuracy,topk=3, name='top3 accuracy')\\\n    .with_regularizer('l2', reg_weight=1e-5)\\\n    .with_model_save_path('Models/effb0_1.pth')\\\n    .with_learning_rate_scheduler(CosineLR(min_lr=1e-5,period=3000))\\\n    .unfreeze_model_scheduling(1,'epoch',module_name='top_conv')\\\n    .unfreeze_model_scheduling(2,'epoch',module_name='block7a')\\\n    .with_automatic_mixed_precision_training()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-06-09T11:08:13.522077Z","iopub.execute_input":"2022-06-09T11:08:13.522633Z","iopub.status.idle":"2022-06-09T11:08:13.551362Z","shell.execute_reply.started":"2022-06-09T11:08:13.522590Z","shell.execute_reply":"2022-06-09T11:08:13.550690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plan=TrainingPlan() \\\n    .add_training_item(effb0) \\\n    .with_data_loader(data_provider)\\\n    .repeat_epochs(10)\\\n    .with_batch_size(32)\\\n    .out_sample_evaluation_scheduling(frequency=100,unit='batch')\\\n    .print_gradients_scheduling(frequency=100,unit='batch')\\\n    .print_progress_scheduling(10,unit='batch') \\\n    .display_loss_metric_curve_scheduling(frequency=200, unit='batch', imshow=True) \\\n    .save_model_scheduling(50,unit='batch')","metadata":{"execution":{"iopub.status.busy":"2022-06-09T11:08:26.281075Z","iopub.execute_input":"2022-06-09T11:08:26.281408Z","iopub.status.idle":"2022-06-09T11:08:26.287388Z","shell.execute_reply.started":"2022-06-09T11:08:26.281380Z","shell.execute_reply":"2022-06-09T11:08:26.286703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plan.start_now()","metadata":{"execution":{"iopub.status.busy":"2022-06-09T11:08:29.910294Z","iopub.execute_input":"2022-06-09T11:08:29.910958Z"},"trusted":true},"execution_count":null,"outputs":[]}]}