{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.8.17","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install fastai","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:47:45.960031Z","iopub.execute_input":"2023-09-08T10:47:45.960769Z","iopub.status.idle":"2023-09-08T10:48:02.673636Z","shell.execute_reply.started":"2023-09-08T10:47:45.960733Z","shell.execute_reply":"2023-09-08T10:48:02.672480Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir=\"/kaggle/input/sorghum-id-fgvc-9/\"","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:59:51.523285Z","iopub.execute_input":"2023-09-08T10:59:51.523709Z","iopub.status.idle":"2023-09-08T10:59:51.528192Z","shell.execute_reply.started":"2023-09-08T10:59:51.523664Z","shell.execute_reply":"2023-09-08T10:59:51.527366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:59:53.268556Z","iopub.execute_input":"2023-09-08T10:59:53.268921Z","iopub.status.idle":"2023-09-08T11:00:15.700963Z","shell.execute_reply.started":"2023-09-08T10:59:53.268890Z","shell.execute_reply":"2023-09-08T11:00:15.699668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 观察可知，train_cultivar_mapping 中存在了图片和其对应的标签，我们先加载这个csv","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv(train_dir+\"train_cultivar_mapping.csv\")\ndf = df[~df['image'].str.contains('.DS_Store')]\ndf.tail(10)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:00:15.786683Z","iopub.execute_input":"2023-09-08T11:00:15.786979Z","iopub.status.idle":"2023-09-08T11:00:15.844404Z","shell.execute_reply.started":"2023-09-08T11:00:15.786952Z","shell.execute_reply":"2023-09-08T11:00:15.843397Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 使用ImageDataLoader 的from_df来加载数据集即可","metadata":{}},{"cell_type":"code","source":"db = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                   splitter=RandomSplitter(valid_pct=0.1),\n                   get_x=ColReader('image', pref=str(train_dir+'train_images')+ os.path.sep),\n                   get_y=ColReader('cultivar'),\n                   item_tfms = Resize(224),\n                   batch_tfms=aug_transforms())","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:00:19.795771Z","iopub.execute_input":"2023-09-08T11:00:19.796249Z","iopub.status.idle":"2023-09-08T11:00:19.808555Z","shell.execute_reply.started":"2023-09-08T11:00:19.796209Z","shell.execute_reply":"2023-09-08T11:00:19.807533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 使用Dataloader进行训练","metadata":{}},{"cell_type":"code","source":"dls=db.dataloaders(df)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:00:22.016811Z","iopub.execute_input":"2023-09-08T11:00:22.017254Z","iopub.status.idle":"2023-09-08T11:00:23.559504Z","shell.execute_reply.started":"2023-09-08T11:00:22.017219Z","shell.execute_reply":"2023-09-08T11:00:23.558330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()#","metadata":{"execution":{"iopub.status.busy":"2023-09-08T10:25:59.844148Z","iopub.execute_input":"2023-09-08T10:25:59.844529Z","iopub.status.idle":"2023-09-08T10:26:05.970223Z","shell.execute_reply.started":"2023-09-08T10:25:59.844497Z","shell.execute_reply":"2023-09-08T10:26:05.968715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)# 构造learner 这里使用ResNet50","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:00:27.502949Z","iopub.execute_input":"2023-09-08T11:00:27.503405Z","iopub.status.idle":"2023-09-08T11:00:28.188195Z","shell.execute_reply.started":"2023-09-08T11:00:27.503365Z","shell.execute_reply":"2023-09-08T11:00:28.187229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()#找到一个合适的学习率'????????","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:00:30.800955Z","iopub.execute_input":"2023-09-08T11:00:30.801373Z","iopub.status.idle":"2023-09-08T11:00:33.546151Z","shell.execute_reply.started":"2023-09-08T11:00:30.801330Z","shell.execute_reply":"2023-09-08T11:00:33.544652Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:39:57.408307Z","iopub.execute_input":"2023-09-08T07:39:57.409260Z","iopub.status.idle":"2023-09-08T07:40:02.853525Z","shell.execute_reply.started":"2023-09-08T07:39:57.409223Z","shell.execute_reply":"2023-09-08T07:40:02.852491Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(8,cbs=[SaveModelCallback(fname='best_model'),ShowGraphCallback()])","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:00:41.271040Z","iopub.execute_input":"2023-09-08T11:00:41.271471Z","iopub.status.idle":"2023-09-08T11:30:02.510164Z","shell.execute_reply.started":"2023-09-08T11:00:41.271435Z","shell.execute_reply":"2023-09-08T11:30:02.508891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T11:48:22.061702Z","iopub.execute_input":"2023-09-08T11:48:22.062175Z","iopub.status.idle":"2023-09-08T11:49:57.700966Z","shell.execute_reply.started":"2023-09-08T11:48:22.062140Z","shell.execute_reply":"2023-09-08T11:49:57.699858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5,lr_max=slice(3e-5,1e-3),cbs=SaveModelCallback(fname='best_model'))","metadata":{"execution":{"iopub.status.busy":"2023-09-08T12:11:25.157396Z","iopub.execute_input":"2023-09-08T12:11:25.157818Z","iopub.status.idle":"2023-09-08T12:41:11.155145Z","shell.execute_reply.started":"2023-09-08T12:11:25.157771Z","shell.execute_reply":"2023-09-08T12:41:11.153876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\nimport random\ntest_dir=\"/kaggle/input/sorghum-id-fgvc-9/test\"\ntest_list = glob(os.path.join(test_dir,\"*\"))\ntest_dl = dls.test_dl(test_list)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T12:41:14.663299Z","iopub.execute_input":"2023-09-08T12:41:14.663774Z","iopub.status.idle":"2023-09-08T12:41:15.909285Z","shell.execute_reply.started":"2023-09-08T12:41:14.663733Z","shell.execute_reply":"2023-09-08T12:41:15.908406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T09:17:18.822309Z","iopub.execute_input":"2023-09-08T09:17:18.822778Z","iopub.status.idle":"2023-09-08T09:17:25.149642Z","shell.execute_reply.started":"2023-09-08T09:17:18.822740Z","shell.execute_reply":"2023-09-08T09:17:25.148694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T12:41:19.650981Z","iopub.execute_input":"2023-09-08T12:41:19.651381Z","iopub.status.idle":"2023-09-08T12:43:52.186391Z","shell.execute_reply.started":"2023-09-08T12:41:19.651347Z","shell.execute_reply":"2023-09-08T12:43:52.185084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds[0][1,:]","metadata":{"execution":{"iopub.status.busy":"2023-09-08T12:45:33.437306Z","iopub.execute_input":"2023-09-08T12:45:33.437727Z","iopub.status.idle":"2023-09-08T12:45:33.446370Z","shell.execute_reply.started":"2023-09-08T12:45:33.437695Z","shell.execute_reply":"2023-09-08T12:45:33.445515Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nnp_preds=np.array(preds[0][:])","metadata":{"execution":{"iopub.status.busy":"2023-09-08T12:48:31.065164Z","iopub.execute_input":"2023-09-08T12:48:31.065614Z","iopub.status.idle":"2023-09-08T12:48:31.071352Z","shell.execute_reply.started":"2023-09-08T12:48:31.065577Z","shell.execute_reply":"2023-09-08T12:48:31.070346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(np_preds)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T12:48:48.948563Z","iopub.execute_input":"2023-09-08T12:48:48.948976Z","iopub.status.idle":"2023-09-08T12:48:48.955550Z","shell.execute_reply.started":"2023-09-08T12:48:48.948942Z","shell.execute_reply":"2023-09-08T12:48:48.954754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_indices = np.argmax(np_preds, axis=1)#怄气\nnp_preds.fill(0)\nfor i, j in enumerate(max_indices):\n    np_preds[i, j] = 1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}