In [1]:
%matplotlib notebook
import numpy as np                                                                 
import matplotlib.pyplot as plt                                                    
import csv                                                                         
import os

In [2]:
data_folder = os.path.expanduser("~")+"/CAE_Project/CAEs/data/"

fig_path = data_folder+"proposed_relu_balle_r_d_curve.pdf"

balle_file = data_folder+"r_d_Balle.csv"
jpeg_file = data_folder+"r_d_JPEG.csv"
jpeg2k_file = data_folder+"r_d_JPEG2k.csv"
                                                                                   
proposed_files = {
    "max_pcm":data_folder+"r_d_proposed_3072_max_compress_pcm.csv",
    "med_pcm":data_folder+"r_d_proposed_7680_med_compress_pcm.csv",
    "min_pcm":data_folder+"r_d_proposed_32768_min_compress_pcm.csv",
    "max_gau":data_folder+"r_d_proposed_3072_max_compress_gauss.csv",
    "med_gau":data_folder+"r_d_proposed_7680_med_compress_gauss.csv",
    "min_gau":data_folder+"r_d_proposed_32768_min_compress_gauss.csv",                                                                                   
    "max_pcm_relu":data_folder+"r_d_proposed_3072_max_compress_pcm_relu.csv",
    "med_pcm_relu":data_folder+"r_d_proposed_7680_med_compress_pcm_relu.csv"}

In [3]:
def csv_to_array(file):
  r_d_list = []                                                            
  with open(file, 'rt') as csvfile:                                        
    reader = csv.reader(csvfile, delimiter=',')                                   
    for row_idx, row in enumerate(reader):                                        
      if row_idx > 0: # first row is header                                       
        r_d_list.append([float(val) if val != "NA" else 0 for val in row])                       
  return(np.array(r_d_list))

In [4]:
def array_means_sems(array, mean_idx=5, rate_idx=2):
    # new shape is [images, compression_levels, metrics (csv_rows)]
    array_reshape = np.array([array[np.where(array[:,0] == idx)]
                              for idx in range(int(np.max(array[:,0]))+1)])
    rates = np.mean(array_reshape[:,:,rate_idx], axis=0) #index 1 is bit/px, index 2 is mem/pix
    means = np.mean(array_reshape[:,:,mean_idx], axis=0) #index 5 is mse
    sems = np.std(array_reshape[:,:,mean_idx], axis=0)/np.sqrt(array_reshape.shape[0])
    return np.stack([rates, means, sems])

In [12]:
jpeg_array = csv_to_array(jpeg_file)
jpeg2k_array = csv_to_array(jpeg2k_file)
balle_array = csv_to_array(balle_file)

In [13]:
dat_keys = ["max_pcm", "med_pcm", "min_pcm"]
proposed_array = np.vstack([csv_to_array(proposed_files[dat_key]) for dat_key in dat_keys])

CSV Index Format:

  • (0) image index
  • (1) bits per pixel
  • (2) memristors per pixel
  • (3) PSNR
  • (4) MS-SSIM
  • (5) MSE

In [15]:
fig = plt.figure()                                                              
plt.scatter(jpeg2k_array[:,2], jpeg2k_array[:,5], s=36, c="g", edgecolors="none", alpha=0.25)
plt.scatter(balle_array[:,2], balle_array[:,5], s=36, c="b", edgecolors="none", alpha=0.15)
plt.scatter(proposed_array[:,2], proposed_array[:,5], s=36, c="r", edgecolors="none", alpha=0.25)
plt.ylabel("MSE")                                                               
plt.xlabel("Memristors Per Pixel")
plt.title("Rate distortions for optimal channel coding\nmethods compared against proposed method")
plt.ylim([0, 400])                                                              
plt.xlim([0, 0.8])                                                              
plt.legend(["JPEG2k_optimal", "Balle_optimal", "Proposed_pcm_dn"])
fig.show()



In [8]:
balle_results = array_means_sems(balle_array, 5, 2)
jpeg2k_results = array_means_sems(jpeg2k_array, 5, 2)
proposed_results = array_means_sems(proposed_array, 5, 2)

fig = plt.figure()                                                              
plt.plot(jpeg2k_results[0], jpeg2k_results[1], c="g", alpha=1)
plt.plot(balle_results[0], balle_results[1], c="b", alpha=1)
plt.plot(proposed_results[0], proposed_results[1], c="r", alpha=1)
plt.ylabel("MSE")                                                               
plt.xlabel("Memristors Per Pixel")    
plt.title("Average Rate Distortions for Optimal Channel Coding vs Proposed")
plt.ylim([0, 400])                                                              
plt.xlim([0, 0.6])                                                              
plt.legend(["JPEG2k_optimal", "Balle_optimal", "Proposed_pcm_dn"])
fig.show()



In [9]:
fig = plt.figure()                                                              
plt.errorbar(jpeg2k_results[0], jpeg2k_results[1], yerr=jpeg2k_results[2], c="g", alpha=1)
plt.errorbar(balle_results[0], balle_results[1], yerr=balle_results[2], c="b", alpha=1)
plt.errorbar(proposed_results[0], proposed_results[1], yerr=proposed_results[2], c="r", alpha=1)
plt.ylabel("MSE")                                                               
plt.xlabel("Memristors Per Pixel")
plt.title("Average Rate Distortions for Optimal Channel Coding vs Proposed\nErrors Indicate SEM On Test Set")
plt.ylim([0, 400])                                                              
plt.xlim([0, 0.6])                                                              
plt.legend(["JPEG2k_optimal", "Balle_optimal", "Proposed_pcm_dn"])
fig.show()



In [10]:
balle_results = array_means_sems(balle_array, 5, 1) # MSE vs bits (binary memristor)
jpeg2k_results = array_means_sems(jpeg2k_array, 5, 1) # MSE vs bits (binary memristor)
proposed_results = array_means_sems(proposed_array, 5, 2) # MSE vs mems

fig = plt.figure()                                                              
plt.errorbar(jpeg2k_results[0], jpeg2k_results[1], yerr=jpeg2k_results[2], c="g", alpha=1)
plt.errorbar(balle_results[0], balle_results[1], yerr=balle_results[2], c="b", alpha=1)
plt.errorbar(proposed_results[0], proposed_results[1], yerr=proposed_results[2], c="r", alpha=1)
plt.ylabel("MSE")
plt.xlabel("Memristors Per Pixel")
plt.ylim([0, 400])
plt.xlim([0, 0.6])
plt.title("Average Rate Distortions for Binary Channel Coding vs Proposed\nErrors Indicate SEM On Test Set")                                                            
plt.legend(["JPEG2k_binary", "Balle_binary", "Proposed_pcm_dn"])
fig.show()



In [11]:
dat_keys = ["max_gau", "med_gau", "min_gau"]
proposed_array = np.vstack([csv_to_array(proposed_files[dat_key]) for dat_key in dat_keys])

balle_results = array_means_sems(balle_array, 5, 1) # MSE vs bits (binary memristor)
jpeg2k_results = array_means_sems(jpeg2k_array, 5, 1) # MSE vs bits (binary memristor)
proposed_results = array_means_sems(proposed_array, 5, 2) # MSE vs mems

fig = plt.figure()                                                              
plt.errorbar(jpeg2k_results[0], jpeg2k_results[1], yerr=jpeg2k_results[2], c="g", alpha=1)
plt.errorbar(balle_results[0], balle_results[1], yerr=balle_results[2], c="b", alpha=1)
plt.errorbar(proposed_results[0], proposed_results[1], yerr=proposed_results[2], c="r", alpha=1)
plt.ylabel("MSE")
plt.xlabel("Memristors Per Pixel")
plt.ylim([0, 400])
plt.xlim([0, 0.6])
plt.title("Average Rate Distortions for Binary Channel Coding vs Proposed\nErrors Indicate SEM On Test Set")                                                            
plt.legend(["JPEG2k", "Balle", "Proposed_gauss_dn"])
fig.show()



In [20]:
dat_keys = ["max_gau", "med_gau", "min_gau"]
proposed_array_gauss = np.vstack([csv_to_array(proposed_files[dat_key]) for dat_key in dat_keys])
dat_keys = ["max_pcm", "med_pcm", "min_pcm"]
proposed_array_pcm = np.vstack([csv_to_array(proposed_files[dat_key]) for dat_key in dat_keys])

balle_results_bin = array_means_sems(balle_array, 5, 1) # MSE vs bits (binary memristor)
balle_results_oracle = array_means_sems(balle_array, 5, 2) # MSE vs mems (2.68 bits/memristor)
proposed_results_gauss = array_means_sems(proposed_array_gauss, 5, 2) # MSE vs mems
proposed_results_pcm = array_means_sems(proposed_array_pcm, 5, 2) # MSE vs mems

fig = plt.figure()    
plt.errorbar(balle_results_bin[0], balle_results_bin[1], yerr=balle_results_bin[2], c="b", alpha=1)
plt.errorbar(balle_results_oracle[0], balle_results_oracle[1], yerr=balle_results_oracle[2], c="g", alpha=1)
plt.errorbar(proposed_results_gauss[0], proposed_results_gauss[1], yerr=proposed_results_gauss[2], c="r", alpha=1)
plt.errorbar(proposed_results_pcm[0], proposed_results_pcm[1], yerr=proposed_results_pcm[2], c="k", alpha=1)
plt.ylabel("MSE")
plt.xlabel("Memristors Per Pixel")
plt.ylim([0, 400])
plt.xlim([0, 0.6])
plt.title("Average Rate Distortions for Binary Channel Coding vs Proposed\nErrors Indicate SEM On Test Set")                                                            
plt.legend(["Balle_bin", "Balle_oracle", "Proposed_gauss_dn", "Proposed_pcm_dn"])
fig.show()



In [22]:
dat_keys = ["max_gau", "med_gau", "min_gau"]
proposed_array_gauss = np.vstack([csv_to_array(proposed_files[dat_key]) for dat_key in dat_keys])
dat_keys = ["max_pcm", "med_pcm", "min_pcm"]
proposed_array_pcm = np.vstack([csv_to_array(proposed_files[dat_key]) for dat_key in dat_keys])

jpeg_results_bin = array_means_sems(jpeg_array, 5, 1) # MSE vs bits (binary memristor)
jpeg_results_oracle = array_means_sems(jpeg_array, 5, 2) # MSE vs mems (2.68 bits/memristor)
proposed_results_gauss = array_means_sems(proposed_array_gauss, 5, 2) # MSE vs mems
proposed_results_pcm = array_means_sems(proposed_array_pcm, 5, 2) # MSE vs mems

fig = plt.figure()    
plt.errorbar(jpeg_results_bin[0], jpeg_results_bin[1], yerr=jpeg_results_bin[2], c="b", alpha=1)
plt.errorbar(jpeg_results_oracle[0], jpeg_results_oracle[1], yerr=jpeg_results_oracle[2], c="g", alpha=1)
plt.errorbar(proposed_results_gauss[0], proposed_results_gauss[1], yerr=proposed_results_gauss[2], c="r", alpha=1)
plt.errorbar(proposed_results_pcm[0], proposed_results_pcm[1], yerr=proposed_results_pcm[2], c="k", alpha=1)
plt.ylabel("MSE")
plt.xlabel("Memristors Per Pixel")
plt.ylim([0, 400])
plt.xlim([0, 0.6])
plt.title("Average Rate Distortions for JPEG Coding vs Proposed\nErrors Indicate SEM On Test Set")                                                            
plt.legend(["JPEG_bin", "JPEG_oracle", "Proposed_gauss_dn", "Proposed_pcm_dn"])
fig.show()



In [ ]: