Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-174
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abdurrahumaan A N
2022-174
Commits
6d68eb3f
Commit
6d68eb3f
authored
Apr 08, 2022
by
nazeerxexagen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
image classification model pipeline
parent
3728851b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
297 additions
and
0 deletions
+297
-0
backend/ImageSearch.ipynb
backend/ImageSearch.ipynb
+297
-0
No files found.
backend/ImageSearch.ipynb
View file @
6d68eb3f
...
...
@@ -155,6 +155,303 @@
" \n",
"setup_seed(seed)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **Custom Pytorch Dataset**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"class Accessoroies(Dataset):\n",
" def __init__(self, transform=None):\n",
" self.data_dir = data_dir\n",
" self.transform = transform\n",
" self.accress_directories()\n",
" \n",
" sample_dict = Counter(self.Labels)\n",
" sub_category_dict_rev = {v : k for k, v in sub_category_dict.items()}\n",
" class_imbalance = {sub_category_dict_rev[k] : v for k, v in sample_dict.items()}\n",
" \n",
" print('##################################### Class Distribution ###########################################')\n",
" print(class_imbalance)\n",
" \n",
" def compute_class_weights(self):\n",
" class_data = dict(Counter(self.Labels))\n",
" class_weights = class_weight.compute_class_weight('balanced',\n",
" classes = np.unique(self.Labels),\n",
" y = self.Labels)\n",
" class_weights = {i : class_weights[i] for i in range(len(set(self.Labels)))}\n",
" class_weights = list(class_weights.values())\n",
" class_weights = torch.FloatTensor(class_weights).cuda()\n",
" return class_weights\n",
"\n",
" def accress_directories(self):\n",
" Images = []\n",
" Labels = []\n",
"\n",
" categories = os.listdir(self.data_dir)\n",
" for category in categories:\n",
" category_dir = os.path.join(data_dir, category)\n",
" sub_categories = os.listdir(category_dir)\n",
" for sub_category in sub_categories:\n",
" sub_category_dir = os.path.join(category_dir, sub_category)\n",
" image_files = os.listdir(sub_category_dir) \n",
" for image_file in image_files:\n",
" image_path = os.path.join(sub_category_dir, image_file)\n",
"\n",
" category_id = category_dict[category]\n",
" sub_category_id = sub_category_dict[sub_category]\n",
" \n",
" # label = [category_id, sub_category_id]\n",
" label = sub_category_id\n",
"\n",
" Images.append(image_path)\n",
" Labels.append(label)\n",
"\n",
"\n",
" Images, Labels = shuffle(Images, Labels)\n",
" self.Images = Images\n",
" self.Labels = Labels\n",
" \n",
" def __len__(self):\n",
" return len(self.Images)\n",
"\n",
" def __getitem__(self, index):\n",
" image_path = self.Images[index]\n",
" label = self.Labels[index]\n",
" \n",
" img = Image.open(image_path).convert('RGB')\n",
" img = np.asarray(img)\n",
"\n",
" if self.transform:\n",
" img = self.transform(img)\n",
"\n",
" label = torch.tensor(label)\n",
" return img, label"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"# image_transform = transforms.Compose([\n",
"# transforms.ToTensor(),\n",
"# transforms.RandomResizedCrop(224),\n",
"# transforms.RandomHorizontalFlip(),\n",
"# transforms.RandomRotation(degrees=180),\n",
"# transforms.Resize(size = input_size)\n",
"# ])\n",
"\n",
"image_transform = transforms.Compose([\n",
" transforms.ToTensor(),\n",
" transforms.RandomHorizontalFlip(),\n",
" transforms.Resize(size = input_size)\n",
" ])\n",
"\n",
"dataset = Accessoroies(image_transform)\n",
"train_dataset, test_dataset = random_split(\n",
" dataset, \n",
" [\n",
" int(len(dataset) * train_size),\n",
" len(dataset) - int(len(dataset) * train_size)\n",
" ])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"train_loader = DataLoader(\n",
" dataset = train_dataset,\n",
" batch_size = batch_size_train ,\n",
" shuffle=True\n",
" )\n",
"\n",
"test_loader = DataLoader(\n",
" dataset = test_dataset,\n",
" batch_size = batch_size_test,\n",
" shuffle=True\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **IMAGE CLASSIFICATION MODEL PIPELINE (PHASE 01)** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **STEP 1 : Build The Image Classification Model**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"def classifier(sequential_flag = True):\n",
" if sequential_flag:\n",
" print('Model Building Using Sequential API ...')\n",
" layers = [\n",
" nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),\n",
" nn.ReLU(),\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" nn.Flatten(),\n",
" nn.Linear(25088, 4096),\n",
" nn.ReLU(),\n",
" nn.Dropout(p=0.5),\n",
" nn.Linear(4096, 4096),\n",
" nn.ReLU(),\n",
" nn.Dropout(p=0.5),\n",
" nn.Linear(4096, 18)\n",
" ]\n",
" model = nn.Sequential(*layers) \n",
" return model\n",
"\n",
"\n",
"\n",
" else:\n",
" print('Model Building Using Functional API ...')\n",
" class ImageSearch(nn.Module):\n",
" def __init__(self):\n",
" super(ImageSearch, self).__init__()\n",
" self.conv1_1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)\n",
" self.conv1_2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1)\n",
"\n",
" self.conv2_1 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1)\n",
" self.conv2_2 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1)\n",
"\n",
" self.conv3_1 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1)\n",
" self.conv3_2 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)\n",
" self.conv3_3 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)\n",
"\n",
" self.conv4_1 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1)\n",
" self.conv4_2 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)\n",
" self.conv4_3 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)\n",
"\n",
" self.conv5_1 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)\n",
" self.conv5_2 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)\n",
" self.conv5_3 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)\n",
"\n",
" self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)\n",
"\n",
" self.fc1 = nn.Linear(25088, 4096)\n",
" self.fc2 = nn.Linear(4096, 4096)\n",
" self.fc3 = nn.Linear(4096, 18)\n",
"\n",
" def forward(self, x):\n",
" x = F.relu(self.conv1_1(x))\n",
" x = F.relu(self.conv1_2(x))\n",
" x = self.maxpool(x)\n",
" x = F.relu(self.conv2_1(x))\n",
" x = F.relu(self.conv2_2(x))\n",
" x = self.maxpool(x)\n",
" x = F.relu(self.conv3_1(x))\n",
" x = F.relu(self.conv3_2(x))\n",
" x = F.relu(self.conv3_3(x))\n",
" x = self.maxpool(x)\n",
" x = F.relu(self.conv4_1(x))\n",
" x = F.relu(self.conv4_2(x))\n",
" x = F.relu(self.conv4_3(x))\n",
" x = self.maxpool(x)\n",
" x = F.relu(self.conv5_1(x))\n",
" x = F.relu(self.conv5_2(x))\n",
" x = F.relu(self.conv5_3(x))\n",
" x = self.maxpool(x)\n",
" x = x.reshape(x.shape[0], -1)\n",
" x = F.relu(self.fc1(x))\n",
" x = F.dropout(x, 0.5)\n",
" x = F.relu(self.fc2(x))\n",
" x = F.dropout(x, 0.5)\n",
" x = self.fc3(x)\n",
" return x\n",
" \n",
" model = ImageSearch()\n",
" \n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"model = classifier()\n",
"torchsummary.summary(model, input_shape)\n",
"model = model.to(device)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **STEP 2 : Define Loss / Optimizer / Metrics**"
]
}
],
"metadata": {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment