-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa4f8d5
commit b3c92e5
Showing
1 changed file
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "44f4b286-2aae-41b5-8bb1-bc6ea37062c6", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import glob\n", | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"from PIL import Image\n", | ||
"\n", | ||
"train_jpg = glob.glob('./农作物朝向检测挑战赛公开数据-初赛/train/*.jpg')\n", | ||
"train_jpg.sort()\n", | ||
"\n", | ||
"test_jpg = glob.glob('./农作物朝向检测挑战赛公开数据-初赛/test/*.jpg')\n", | ||
"test_jpg.sort()\n", | ||
"\n", | ||
"train_txt = glob.glob('./农作物朝向检测挑战赛公开数据-初赛/train/*.txt')\n", | ||
"train_txt.sort()\n", | ||
"train_txt = np.array([open(x).readlines() for x in train_txt]).astype(np.float32)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "e688aa3c-3bf4-42af-a19b-3291689f584b", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"import torchvision\n", | ||
"import torchvision.transforms as transforms\n", | ||
"\n", | ||
"# Define the transform\n", | ||
"transform = transforms.Compose([\n", | ||
" transforms.Resize((224, 224)), # Resize the image to 224x224 pixels\n", | ||
" transforms.ToTensor() # Convert the image to a PyTorch tensor\n", | ||
"])\n", | ||
"\n", | ||
"model = torchvision.models.resnet18(weights='ResNet18_Weights.DEFAULT')\n", | ||
"model.fc = torch.nn.Identity()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "66108968-019d-451c-8559-8fa7ddf9695b", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"train_feat = []\n", | ||
"for path in train_jpg:\n", | ||
" img = Image.open(path)\n", | ||
" img = transform(img)\n", | ||
" with torch.no_grad():\n", | ||
" feat = model(img[None, :, :, :])\n", | ||
" train_feat.append(feat)\n", | ||
" \n", | ||
"train_feat = torch.vstack(train_feat)\n", | ||
"train_feat = train_feat.data.numpy()\n", | ||
"\n", | ||
"\n", | ||
"test_feat = []\n", | ||
"for path in test_jpg:\n", | ||
" img = Image.open(path)\n", | ||
" img = transform(img)\n", | ||
" with torch.no_grad():\n", | ||
" feat = model(img[None, :, :, :])\n", | ||
" test_feat.append(feat)\n", | ||
" \n", | ||
"test_feat = torch.vstack(test_feat)\n", | ||
"test_feat = test_feat.data.numpy()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "0e271e39-5d0b-4e25-a08a-34dc8e4b8bb5", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LinearRegression()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">LinearRegression</label><div class=\"sk-toggleable__content\"><pre>LinearRegression()</pre></div></div></div></div></div>" | ||
], | ||
"text/plain": [ | ||
"LinearRegression()" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from sklearn.linear_model import LinearRegression\n", | ||
"m = LinearRegression()\n", | ||
"m.fit(train_feat, train_txt)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "05c28168-0db5-42c2-abb3-7081052af558", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import shutil, os\n", | ||
"if os.path.exists('submit'):\n", | ||
" shutil.rmtree('submit')\n", | ||
" \n", | ||
"os.mkdir('submit')\n", | ||
"\n", | ||
"for path, feat in zip(test_jpg, m.predict(test_feat)):\n", | ||
" up = open('./submit/' + os.path.basename(path)[:-4] + '.txt', 'w')\n", | ||
" for f in feat:\n", | ||
" up.write(str(f) + '\\n')\n", | ||
" up.close()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "de31f739-22e6-469b-84f2-815473098c31", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"updating: submit/ (stored 0%)\n", | ||
"updating: submit/00000195.txt (deflated 3%)\n", | ||
"updating: submit/00000192.txt (stored 0%)\n", | ||
"updating: submit/00000162.txt (stored 0%)\n", | ||
"updating: submit/00000165.txt (stored 0%)\n", | ||
"updating: submit/00000156.txt (stored 0%)\n", | ||
"updating: submit/00000151.txt (stored 0%)\n", | ||
"updating: submit/00000173.txt (stored 0%)\n", | ||
"updating: submit/00000170.txt (stored 0%)\n", | ||
"updating: submit/00000161.txt (stored 0%)\n", | ||
"updating: submit/00000168.txt (deflated 3%)\n", | ||
"updating: submit/00000159.txt (stored 0%)\n", | ||
"updating: submit/00000176.txt (deflated 3%)\n", | ||
"updating: submit/00000158.txt (stored 0%)\n", | ||
"updating: submit/00000183.txt (stored 0%)\n", | ||
"updating: submit/00000198.txt (stored 0%)\n", | ||
"updating: submit/00000197.txt (stored 0%)\n", | ||
"updating: submit/00000174.txt (stored 0%)\n", | ||
"updating: submit/00000155.txt (stored 0%)\n", | ||
"updating: submit/00000184.txt (stored 0%)\n", | ||
"updating: submit/00000186.txt (stored 0%)\n", | ||
"updating: submit/00000166.txt (stored 0%)\n", | ||
"updating: submit/00000169.txt (stored 0%)\n", | ||
"updating: submit/00000154.txt (stored 0%)\n", | ||
"updating: submit/00000199.txt (deflated 6%)\n", | ||
"updating: submit/00000178.txt (stored 0%)\n", | ||
"updating: submit/00000185.txt (deflated 3%)\n", | ||
"updating: submit/00000180.txt (deflated 6%)\n", | ||
"updating: submit/00000177.txt (deflated 3%)\n", | ||
"updating: submit/00000182.txt (deflated 3%)\n", | ||
"updating: submit/00000164.txt (stored 0%)\n", | ||
"updating: submit/00000189.txt (stored 0%)\n", | ||
"updating: submit/00000181.txt (stored 0%)\n", | ||
"updating: submit/00000153.txt (stored 0%)\n", | ||
"updating: submit/00000193.txt (deflated 13%)\n", | ||
"updating: submit/00000175.txt (stored 0%)\n", | ||
"updating: submit/00000152.txt (stored 0%)\n", | ||
"updating: submit/00000171.txt (stored 0%)\n", | ||
"updating: submit/00000187.txt (stored 0%)\n", | ||
"updating: submit/00000172.txt (stored 0%)\n", | ||
"updating: submit/00000167.txt (stored 0%)\n", | ||
"updating: submit/00000163.txt (stored 0%)\n", | ||
"updating: submit/00000188.txt (stored 0%)\n", | ||
"updating: submit/00000160.txt (deflated 6%)\n", | ||
"updating: submit/00000196.txt (stored 0%)\n", | ||
"updating: submit/00000194.txt (deflated 3%)\n", | ||
"updating: submit/00000191.txt (stored 0%)\n", | ||
"updating: submit/00000179.txt (deflated 6%)\n", | ||
"updating: submit/00000150.txt (stored 0%)\n", | ||
"updating: submit/00000190.txt (stored 0%)\n", | ||
"updating: submit/00000157.txt (stored 0%)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!zip -r submit.zip submit" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8fc967df-ef40-4aa2-b53e-2fe7ff3c347f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3.10" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.10" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |