-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert data set to CSV with custom resize #90
Open
umarkhan29
wants to merge
1
commit into
ieee8023:master
Choose a base branch
from
umarkhan29:umarkhan29-GetCSVDataSet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,85 @@ | ||
''' | ||
This code creates a CSV format of data set with resized images of your defined size (Height, Width) | ||
We got labels for every image from metadata.csv and in new creted dataset, first attribute defines the label | ||
label == 1 defines COVID-19 and Label == 0 defines all others | ||
Since Excel contains XFD as last column and XFD equals 16384. Thus we have to look for the resized height and width | ||
In this example we took 40 x 40 with 3 channels making total row size (40x40x3)+1 +1 is added because label is stored as first attribute of image | ||
|
||
''' | ||
import numpy as np | ||
import pandas as pd | ||
import csv | ||
from keras.preprocessing.image import ImageDataGenerator | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
#Defining image size | ||
Height = 40 | ||
Width = 40 | ||
|
||
|
||
#defining dataset dir | ||
DATASET_DIR = "./dataset/Images/" #Define paths as per your environment | ||
metadata = "./dataset/metadata/metadata.csv" # Meta info - path to metadata file | ||
|
||
|
||
# Resizing Training data images to 40 x 40 | ||
train_datagen = ImageDataGenerator() | ||
train_generator = train_datagen.flow_from_directory( | ||
DATASET_DIR, | ||
target_size=(Height, Width), | ||
batch_size=1, | ||
class_mode='binary', | ||
subset='training') | ||
|
||
|
||
#Getting Labels | ||
metadata_csv = pd.read_csv(metadata) | ||
labels = [] | ||
for file in train_generator.filenames: | ||
for (x, row) in metadata_csv.iterrows(): | ||
file_name = 'images\\'+ row["filename"] #file contains the directory path 'images\' as well. Thus prefixing images\ to row['filename'] for comparison | ||
if file_name == file: | ||
label = row['finding'] | ||
if label == "COVID-19": | ||
label = 1 #label == 1 defines COVID-19 | ||
else: | ||
label = 0 | ||
break | ||
labels.append(label) | ||
|
||
print('Total Images: ',len(labels)) | ||
|
||
|
||
#Writting pixel values to CSV file | ||
with open('mydata.csv', 'w', newline='') as file: | ||
writer = csv.writer(file) | ||
for i in range(len(train_generator)): | ||
image, label = train_generator[i] | ||
image = image.reshape(Height*Width*3) # Reshaping into single list | ||
label = labels[i] | ||
row = [label] #Writting label as first element of the row | ||
for i in range(len(image)): #Iterating over Height | ||
row.append(image[i]) #Adding all pixels of an image in a single row after the label | ||
writer.writerow(row) | ||
|
||
print("CSV data genereated sucessfully !"); | ||
|
||
|
||
# Plot the first image from the input dataset to be sure that data is correctly transformed | ||
#============================================================ | ||
# Take a single image, and remove the color dimension by reshaping | ||
imgSize = Height*Width*3 + 1 #We used first element as label | ||
image = row[1:imgSize] #Taking Image pixels from element 2 | ||
image = np.array(image) | ||
image = image.reshape(Height,Width,3) | ||
|
||
|
||
#Plotting here | ||
plt.figure() | ||
plt.imshow(image.astype(int), cmap=plt.cm.binary) | ||
plt.colorbar() | ||
plt.grid(False) | ||
plt.title('First Image') | ||
plt.show() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All your code is perfectly clean and this small line is making noise to it.