I am doing a CNN project to estimate pitch from a spectrogram. The project is already finished and waiting to be presented to my institution, but I would like to improve a little detail to my work.
CNN that I have built must be tested using test (val) datas. I store the data in Google Drive (I build my CNN using Google Colab), and before doing testing with do_test method, I have to load the data. Path are given in the code snippet below.
I am able to load the data. The loaded data is then tested, but my problem is, I do not know which file that I have loaded. Testing result is exported to a Python DataFrame. DataFrame example output is attached here.
I want to put the filename of each file loaded and then put it to Dataframe output result, so I will know each output of a specific file. Right now, I can only know the overall data from 1 song without knowing which file is which (see DataFrame). What should I add to my code to get the filenames? Should I modify image_data code in function get_image_and_label?
Some (I hope) useful items:
- Folder containing image. Folder link is given here
- Folder containing labels can be accessed here
- DataFrame output screenshot

Method do_test. This method is used to execute testing.
model = Sequential()
song_titles = "014_twinkle_twinkle 017_old_mcdonald 020_happy_birthday 022_brother_john 030_london_bridge".split()
image_data_train_global_path = "./drive/MyDrive/1117002_Code Skripsi/Dataset/Dataset TA_1_spectrograms/spectrogram_B_2/train/"
image_data_test_global_path = "./drive/MyDrive/1117002_Code Skripsi/Dataset/Dataset TA_1_spectrograms/spectrogram_B_2/val/"
label_global_path = "./drive/MyDrive/1117002_Code Skripsi/Dataset/Dataset TA_2_label_floor/"
def do_test():
for title in song_titles:
print("Song title: ", title)
label_and_image_test = load_image_and_label(title, process='test') # this is where I load my data.
images_test, labels_test = populate_test_data(label_and_image_test)
X_test, y_test = label_processing(images_test, labels_test)
y_pred = model.predict(X_test)
song_number = 0
unique_pitch_result = []
pitch_class_result = []
distance_result = []
while song_number <= (len(y_pred)-1):
label_sequence = []
for i in y_pred[song_number]:
max = np.max(i)
ind = np.argmax(i)
label_sequence.append(ind)
result_pitch_sequence = [k for element in label_sequence for k,v in unique_pitch_dictionary.items() if element == v]
unique_pitch = np.unique(result_pitch_sequence)
unique_pitch = unique_pitch.tolist()
if (title=="014_twinkle_twinkle"):
s1 = s1_014_twinkle
elif (title=="017_old_mcdonald"):
s1 = s1_017_mcd
elif (title=="020_happy_birthday"):
s1 = s1_020_hbd
elif (title=="022_brother_john"):
s1 = s1_022_john
elif (title=="030_london_bridge"):
s1 = s1_030_london
cost_matrix = DTW.dtw_cost_matrix(s1, result_pitch_sequence)
distance = DTW.dtw_distance(cost_matrix)
pitch_class = [v for element in unique_pitch for k,v in pitch_class_dictionary.items() if element == k]
unique_pitch_result.append(unique_pitch)
pitch_class_result.append(pitch_class)
distance_result.append(distance)
song_number+=1
print("+"*100)
df = DTW.test_result_dataframe(unique_pitch_result, pitch_class_result, distance_result, song_number)
print(df)
filename = title + " " + str(epochs) + " " + str(learning_rate) + " dataframe.xlsx"
df.to_excel(filename)
print("*"*120)
Function load_image_and_label. This is used to get data path, then execute file loading from directory.
def load_image_and_label(title, process):
print("Title: ", title)
label_specific_path = label_global_path + title
if (process == "train-test"):
image_data_train_specific_path = image_data_train_global_path + title
image_training_data = get_image_and_label(image_data_train_specific_path, label_specific_path)
image_data_test_specific_path = image_data_test_global_path + title
image_testing_data = get_image_and_label(image_data_test_specific_path, label_specific_path)
if (process == "train-test"):
return image_training_data, image_testing_data
else:
return image_testing_data
Function get_image_and_label. This is where I try to get my files. I think I have to modify something here to output the filename.
def get_image_and_label(image_path, label_path):
print("Getting image data from: ", image_path)
image_data = [] # contains array of loaded image and its label. Maybe, should I add code to append the filename here?
tm = time.time()
for img_item in os.listdir(image_path): #for every image in path
try:
img_array = cv2.imread(os.path.join(image_path, img_item))
# preprocess image
spectrogram_preprocessing = resize_recolor_spectrogram(img_array)
# imread to array
spectrogram_preprocessing = np.array(spectrogram_preprocessing)
# image shape - transpose
spectrogram_preprocessing = np.transpose(spectrogram_preprocessing)
# extract image labels
label = extract_pitch_label(os.path.join(label_path, img_item))
image_data.append([spectrogram_preprocessing, label]) # add image data and its label to image_data array. What code should I add here to get filename?
except Exception as e:
raise e
print("Got "+str(len(image_data))+" images from " + image_path + " in "+str(time.time() - tm)+ " seconds")
print("-"*110)
return image_data
This is the output if I have succeeded in loading my data.
Song title: 014_twinkle_twinkle
Title: 014_twinkle_twinkle
Getting image data from: ./drive/MyDrive/1117002_Code Skripsi/Dataset/Dataset TA_1_spectrograms/spectrogram_B_2/val/014_twinkle_twinkle
Got 32 images from ./drive/MyDrive/1117002_Code Skripsi/Dataset/Dataset TA_1_spectrograms/spectrogram_B_2/val/014_twinkle_twinkle in 0.29237914085388184 seconds
---------------------------------------------------------------------------------------------------------