Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
212 changes: 212 additions & 0 deletions machine_learning/Gender_Classifiacation/Gender Class.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "bc968054-3bbb-4aea-acee-d3ea85b8052f",
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"from tensorflow.keras import layers, models\n",
"import numpy as np\n",
"from tensorflow.keras.preprocessing import image"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abe0d9f2-5b25-4e25-b896-0261689c5910",
"metadata": {},
"outputs": [],
"source": [
"train_dir = r\"Training_dir\"\n",
"val_dir = r\"Validation_dir\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "72e33751-c2f7-488e-951a-356bd4eee573",
"metadata": {},
"outputs": [],
"source": [
"train_datagen = ImageDataGenerator(rescale = 1./225)\n",
"val_datagen = ImageDataGenerator(rescale = 1./225)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6f4f22c6-4d1a-4833-9edc-781704dd95ff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 47009 images belonging to 2 classes.\n",
"Found 11649 images belonging to 2 classes.\n"
]
}
],
"source": [
"train_generator = train_datagen.flow_from_directory(\n",
" train_dir,\n",
" target_size = (150, 150),\n",
" batch_size = 32,\n",
" class_mode = \"binary\"\n",
")\n",
"validation_generator = val_datagen.flow_from_directory(\n",
" val_dir,\n",
" target_size = (150, 150),\n",
" batch_size = 32,\n",
" class_mode = \"binary\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d2cc1c68-4d51-445b-afd9-5f80f89353fd",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\ombaj\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:113: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
" super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n"
]
}
],
"source": [
"model = models.Sequential([\n",
" layers.Conv2D(32, (3, 3), activation = \"relu\", input_shape = (150, 150, 3)),\n",
" layers.MaxPooling2D(2, 2),\n",
" layers.Conv2D(64, (3,3), activation = \"relu\"),\n",
" layers.MaxPooling2D(2, 2),\n",
" layers.Conv2D(128, (3, 3), activation = \"relu\"),\n",
" layers.MaxPooling2D(2, 2),\n",
" layers.Flatten(),\n",
" layers.Dense(512, activation = \"relu\"),\n",
" layers.Dense(1, activation = \"sigmoid\")\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e2751754-9bad-40d9-ad64-09897dcd456a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:TensorFlow GPU support is not available on native Windows for TensorFlow >= 2.11. Even if CUDA/cuDNN are installed, GPU will not be used. Please use WSL2 or the TensorFlow-DirectML plugin.\n"
]
}
],
"source": [
"model.compile(\n",
" optimizer = \"adam\",\n",
" loss = \"binary_crossentropy\",\n",
" metrics = [\"accuracy\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "bd8c8820-ef2a-40fb-aa64-463a3c81e246",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/5\n",
"\u001b[1m 14/1470\u001b[0m \u001b[37m━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[1m6:47\u001b[0m 280ms/step - accuracy: 0.4598 - loss: 1.3940"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"KeyboardInterrupt\n",
"\n"
]
}
],
"source": [
"Trained_model = model.fit(\n",
" train_generator,\n",
" epochs = 5,\n",
" validation_data = validation_generator\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "baf947b4-b5c0-441b-9fe6-fa9a7773309f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 179ms/step\n",
"Prediction: Female\n"
]
}
],
"source": [
"def predict_gender(path):\n",
" img = image.load_img(path, target_size=(150, 150))\n",
" x = image.img_to_array(img) / 255.0 \n",
" x = np.expand_dims(x, axis=0) \n",
"\n",
" result = model.predict(x)\n",
" if result[0] > 0.5:\n",
" print(\"Prediction: Male\")\n",
" else:\n",
" print(\"Prediction: Female\")\n",
"\n",
"predict_gender(r'YOUR_IMAGE_PATH')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8fb4ec5-5c6b-4ba0-806a-a525de92529f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.11 (Stable)",
"language": "python",
"name": "python311"
},
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading