Skip to content

Commit

Permalink
Added code
Browse files Browse the repository at this point in the history
  • Loading branch information
charanya78 committed Oct 29, 2022
1 parent 286c437 commit 5ec018b
Show file tree
Hide file tree
Showing 3,963 changed files with 25,559 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
789 changes: 789 additions & 0 deletions .ipynb_checkpoints/1. CNN-checkpoint.ipynb

Large diffs are not rendered by default.

178 changes: 178 additions & 0 deletions .ipynb_checkpoints/2. Hair removal-checkpoint.ipynb

Large diffs are not rendered by default.

313 changes: 313 additions & 0 deletions .ipynb_checkpoints/3. Data Augmentation-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "9990b7ea",
"metadata": {},
"source": [
"# Data Augmentation process\n",
"\n",
"### This file has the data augmentation process. The steps performed are:\n",
"\n",
"1. To balance the BSD data which has a higher portion of malignant samples, data augmentation is performed on the benign samples.\n",
"2. The augmentation techniques include flipping, random noise addition and random rotation "
]
},
{
"cell_type": "markdown",
"id": "6173d8e3",
"metadata": {},
"source": [
"Number of images in malignant class"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5213e016",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"313\n"
]
}
],
"source": [
"import os\n",
"\n",
"list1 = os.listdir(\"C:/Users/Kannan-PC/Desktop/Projects/melanoma/Final Year Project/final/malignant\") \n",
"\n",
"number_files1 = len(list1)\n",
"print(number_files1)"
]
},
{
"cell_type": "markdown",
"id": "0e22a997",
"metadata": {},
"source": [
"Number of images in benign class"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "43bbcdd8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"213\n"
]
}
],
"source": [
"list2 = os.listdir(\"C:/Users/Kannan-PC/Desktop/Projects/melanoma/Final Year Project/final/benign\") \n",
"\n",
"number_files2 = len(list2)\n",
"print(number_files2)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "c54f6af3",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import random\n",
"from scipy import ndarray\n",
"import skimage as sk\n",
"from skimage import transform\n",
"from skimage import util\n",
"from skimage import io"
]
},
{
"cell_type": "markdown",
"id": "ea6589d4",
"metadata": {},
"source": [
"Defining rotation , flipping and noise functions"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "8bc562c4",
"metadata": {},
"outputs": [],
"source": [
"def rotation(image_array: ndarray):\n",
" random_degree = random.uniform(-25, 25)\n",
" return sk.transform.rotate(image_array, random_degree)\n",
"\n",
"def noise(image_array: ndarray):\n",
" return sk.util.random_noise(image_array)\n",
"\n",
"def flip(image_array: ndarray):\n",
" return image_array[:, ::-1]"
]
},
{
"cell_type": "markdown",
"id": "2b852582",
"metadata": {},
"source": [
"Defining the available transformations in a dictionary"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "95bf9a59",
"metadata": {},
"outputs": [],
"source": [
"available_transformations = {\n",
" 'rotate': random_rotation,\n",
" 'noise': random_noise,\n",
" 'horizontal_flip': horizontal_flip\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "4aaa2287",
"metadata": {},
"source": [
"Initializing the directory path and the number of images to be generated. Declare an array containing all the names of the benign images"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "536fee72",
"metadata": {},
"outputs": [],
"source": [
"folder_path = \"C:/Users/Kannan-PC/Desktop/Projects/melanoma/Final Year Project/final/benign\"\n",
"num_files_desired = 100\n",
"images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]"
]
},
{
"cell_type": "markdown",
"id": "8c97bd0e",
"metadata": {},
"source": [
"Transforming the images till the generated images reach 100"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "4c678e9b",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n",
"Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.\n"
]
}
],
"source": [
"num_generated_files = 0\n",
"\n",
"while num_generated_files <= num_files_desired:\n",
" \n",
" image_path = random.choice(images)\n",
" image_to_transform = sk.io.imread(image_path)\n",
" num_transformations_to_apply = random.randint(1, len(available_transformations))\n",
" num_transformations = 0\n",
" transformed_image = None\n",
" \n",
" while num_transformations <= num_transformations_to_apply:\n",
" \n",
" key = random.choice(list(available_transformations.keys()))\n",
" transformed_image = available_transformations[key](image_to_transform)\n",
" num_transformations += 1\n",
"\n",
" new_file_path = '%s/augmented_image_%s.jpg' % (folder_path, num_generated_files)\n",
" io.imsave(new_file_path, transformed_image)\n",
" num_generated_files += 1"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "68253905",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"314\n"
]
}
],
"source": [
"list2 = os.listdir(\"C:/Users/Kannan-PC/Desktop/Projects/melanoma/Final Year Project/final/benign\") \n",
"\n",
"number_files2 = len(list2)\n",
"print(number_files2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 5ec018b

Please sign in to comment.