<jupyter_start><jupyter_text>**ML Exercise 3** **<NAME>    <NAME>**<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
from scipy import stats, ndimage, misc
import cv2
positive_names = ['p0'+str(i)+'.png' if i<10 else 'p'+str(i)+'.png' for i in np.arange(1,31)]
negative_names = ['n0'+str(i)+'.png' if i<10 else 'n'+str(i)+'.png' for i in np.arange(1,31)]

positive_ims = [np.array(Image.open(img)) for img in positive_names]
negative_ims = [np.array(Image.open(img)) for img in negative_names]

#positive_ims = [np.array(cv2.imread(img)) for img in positive_names]
#negative_ims = [np.array(cv2.imread(img)) for img in negative_names]
fig, ax = plt.subplots(6,10, figsize=(14, 6), sharey=True)
for i in range(len(positive_ims)):
  ax[int(i/10)][i%10].imshow(positive_ims[i])
for j in range(len(negative_ims)):
  counter = j + i + 1
  ax[int(counter/10)][counter%10].imshow(negative_ims[j])  
plt.show()

def get_grayscaleImage(img):
  gray =  np.dot(img, [299, 587, 114])/1000
  return gray.astype('int32')

def apply_sobel(grayscale_image):
  im = grayscale_image.astype('int32')
  dx = ndimage.sobel(im, 1)  # horizontal derivative
  dy = ndimage.sobel(im, 0)  # vertical derivative
  mag = np.hypot(dx, dy)  # magnitude
  return mag.astype('int32')

def get_binary_blob(im):
  im = cv2.imread(im, cv2.IMREAD_GRAYSCALE)
  detector = cv2.SimpleBlobDetector_create()
  keypoints = detector.detect(im)
  im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
  ret,thresh_img = cv2.threshold(im_with_keypoints,127,255,cv2.THRESH_BINARY)
  return im_with_keypoints

def get_canny_edge_detector(imgs):
  im = cv2.imread(imgs, cv2.IMREAD_GRAYSCALE)
  return cv2.Canny(im,100,200)

positive_blobs = [get_binary_blob(imageName) for imageName in positive_names]
negative_blobs = [get_binary_blob(imageName) for imageName in negative_names]

positive_edges = [get_canny_edge_detector(imageName) for imageName in positive_names]
negative_edges = [get_canny_edge_detector(imageName) for imageName in negative_names]

gray_positives = get_grayscaleImage(positive_ims)
sobel_positives = apply_sobel(gray_positives)

gray_negatives = get_grayscaleImage(negative_ims)
sobel_negatives = apply_sobel(gray_negatives)

fig, ax1 = plt.subplots(30,10, figsize=(14, 30), sharey=True)
for i in range(len(positive_ims)):
  ax1[int(i/10)*5][i%10].imshow(positive_ims[i])
  ax1[int(i/10)*5 + 1][i%10].imshow(gray_positives[i], cmap='gray')
  ax1[int(i/10)*5 + 2][i%10].imshow(sobel_positives[i], cmap='gray')
  ax1[int(i/10)*5 + 3][i%10].imshow(positive_blobs[i])
  ax1[int(i/10)*5 + 4][i%10].imshow(positive_edges[i],  cmap='gray')
for j in range(len(negative_ims)):
  counter = j + i + 1
  ax1[int(counter/10)*5][counter%10].imshow(negative_ims[j]) 
  ax1[int(counter/10)*5 + 1][counter%10].imshow(gray_negatives[j], cmap='gray')
  ax1[int(counter/10)*5 + 2][counter%10].imshow(sobel_negatives[j], cmap='gray')
  ax1[int(counter/10)*5 + 3][counter%10].imshow(negative_blobs[j])
  ax1[int(counter/10)*5 + 4][counter%10].imshow(negative_edges[j],  cmap='gray')
  
pos_mean = np.mean(positive_ims, axis=(0,1,2))
neg_mean = np.mean(negative_ims, axis=(0,1,2))

sobel_pos_mean = np.mean(sobel_positives)
sobel_neg_mean = np.mean(sobel_negatives)

edge_pos_mean = np.mean(positive_edges)
edge_neg_mean = np.mean(negative_edges)

#gray_pos_mean = np.mean(gray_positives)
#gray_neg_mean = np.mean(gray_negatives)


#print(np.max(gray_positives[1]), np.min(gray_positives[1]))
#print(np.max(sobel_positives[1]), np.min(sobel_positives[1]))
print(np.shape(pos_mean))
print(pos_mean)
print(np.shape(neg_mean))
print(neg_mean)
print(sobel_pos_mean)
print(sobel_neg_mean)

print(edge_pos_mean)
print(edge_neg_mean)

phi = len(positive_ims)/ (len(positive_ims) + len(negative_ims))
print('phi is ',phi)
#mu_0 = pos_mean
#mu_1 = neg_mean

mu_0 = np.append(pos_mean, np.var(positive_ims, axis=(0,1,2), dtype='float32'))
mu_1 = np.append(neg_mean, np.var(negative_ims, axis=(0,1,2), dtype='float32'))

#mu_0 = np.append(mu_0, [np.mean(positive_ims), np.var(positive_ims)])
#mu_1 = np.append(mu_1, [np.mean(negative_ims), np.var(negative_ims)])

#mu_0 = np.append(mu_0, gray_pos_mean)
#mu_1 = np.append(mu_1, gray_neg_mean)

mu_0 = np.append(mu_0, edge_pos_mean)
mu_1 = np.append(mu_1, edge_neg_mean)

#mu_0 = np.append(mu_0, sobel_pos_mean)
#mu_1 = np.append(mu_1, sobel_neg_mean)

print('mu_0 is ',mu_0)
print('mu_1 is ',mu_1)


def calc_sigma(data, edge_images, pos_mean):
  overall_avg = np.mean(data, axis=(1,2,3))
  overall_variance = np.var(data, axis=(1,2,3))
  channel_avg = np.mean(data, axis=(1,2))
  channel_variance = np.var(data, axis=(1,2))
  #sobel_avg = np.mean(apply_sobel(get_grayscaleImage(data)), axis=(1,2))
  #sobel_var = np.var(apply_sobel(get_grayscaleImage(data)), axis=(1,2))
  edge_avg = np.mean(edge_images, axis=(1,2))
  #data_temp = np.column_stack((channel_variance, sobel_avg))
  #data_temp = np.column_stack((channel_variance, sobel_avg, sobel_var))
  data_temp = np.column_stack((channel_variance, edge_avg))
  data_aug =np.append(channel_avg, data_temp, axis=1)
  temp_dot = np.dot((data_aug - pos_mean).T, data_aug - pos_mean)
  #print(np.shape(temp_dot))
  #print(temp_dot)
  return temp_dot

sigma = (1/(len(positive_ims) + len(negative_ims))) * (calc_sigma(positive_ims, positive_edges, mu_0) + calc_sigma(negative_ims, negative_edges, mu_1))
print(np.shape(sigma))
print(sigma)
print(np.linalg.det(sigma))
def calculateGDA(x, mu_0, mu_1, sigma):
  n = len(mu_0) if len(mu_0) else 1 
  
  sigma_deter = np.linalg.det(sigma)
  #print(sigma_deter)
  multiplier_coff = 1/(np.power(2*np.pi, n/2)* np.power(sigma_deter,0.5))
  #print(multiplier_coff)
  #print(np.linalg.inv(sigma))
  expo_temp_0 = np.dot(np.linalg.inv(sigma),x - mu_0)
  expo_temp_1 = np.dot(np.linalg.inv(sigma),x - mu_1)
  expo_0 = np.dot((x - mu_0).T, expo_temp_0)
  expo_1 = np.dot((x - mu_1).T, expo_temp_1)
  #print('expo_0 is ', expo_0)
  #print('expo_0 is ', np.exp(-1/2 * expo_0))
  return [multiplier_coff*np.exp(-1/2 * expo_0), multiplier_coff*np.exp(-1/2 * expo_1)]
  
                     
np.set_printoptions(formatter={'float': '{: 0.10f}'.format})

def calc_prob(img, img_edge):
  #d = np.mean(img)
  #d = np.mean(img, axis=(0,1))
  #d = np.append(np.mean(img, axis=(0,1)), [np.mean(img), np.var(img)])
  d = np.append(np.mean(img, axis=(0,1)), np.var(img, axis=(0,1)))
  #d = np.append(d, np.mean(apply_sobel(get_grayscaleImage(img))))
  #d = np.append(d, np.var(apply_sobel(get_grayscaleImage(img))))
  d = np.append(d, np.mean(img_edge))
  #print(d)
  return calculateGDA(d, mu_0, mu_1, sigma)
#np.shape(np.mean(positive_ims[0], axis=(0,1)))
pos_probs = [calc_prob(positive_ims[i], positive_edges[i]) for i in range(30)]
#print(filter(pos_probs, lambda x: x[0] < x[1]))
print('<-----------------------  POSITIVES  -------------------------->')
print(np.array(pos_probs) * 100000000000000000)
print('<-----------------------  NEGATIVES  -------------------------->')
neg_probs = [calc_prob(negative_ims[i], negative_edges[i]) for i in range(30)]
print(np.array(neg_probs) * 100000000000000000)


print('<------------------positive stats--------------->')
pos_probs_test = [np.append(x, ind + 1) for ind,x in enumerate(pos_probs)]
l_0 = list(filter(lambda x: x[0] <= x[1], pos_probs_test))
print(np.array(l_0))
print('<------------------negative stats--------------->')
neg_probs_test = [np.append(x, ind + 1) for ind,x in enumerate(neg_probs)]
l_1 = list(filter(lambda x: x[0] >= x[1], neg_probs_test))
print(np.array(l_1))<jupyter_output><empty_output><jupyter_text>REPORT We are using following 6 features and appy GDA for classification taskMean of color in each channel(R,G,B)Variance of color in each Channel(R,G,B).Based of the learned feature, the model is able to correctly classifying all the images except one negative example(number 6 )<jupyter_code>#t = get_grayscaleImage(positive_ims)
#test = get_binary_blob('p01.png')
#test = get_binary_blob(gray_positives[0])

plt.imshow(test[0], cmap='gray')
#ax[0][1].imshow(im, cmap='gray')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18400488400488402, 'fraction_numerical': 0.040170940170940174, 'mean_word_length': 5.621665319320938, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '481141', 'license': 'MIT', 'n_tokens_mistral': 3591, 'n_tokens_neox': 3379, 'n_words': 560, 'path': 'Results/Jupyter/ML/ML_SoSe19/ML_SoSe19-master/ML_ex3.ipynb', 'repo_name': 'in2dblue/interactive-rl', 'size': '8211'}