Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import os
  2. os.environ["CUDA_VISIBLE_DEVICES"]="-1"
  3. import tensorflow as tf
  4. import numpy as np
  5. import cv2
  6. import matplotlib.pyplot as plt
  7. #from PIL import Image
  8. #import time
  9. tf.compat.v1.enable_eager_execution()
  10.  
  11. #config = tf.ConfigProto()
  12. #config.intra_op_parallelism_threads = 1
  13. #config.inter_op_parallelism_threads = 1
  14.  
  15. #tf.keras.backend.set_session(tf.Session(config=config))
  16.  
  17. IMG_SIZE = 250
  18. fashion_filepath = 'model/Fashion_niche_resnet50_model_wo_opt.hdf5'
  19. beauty_health_filepath = 'model/Beauty_Health_niche_resnet50_model_wo_opt.hdf5'
  20. food_beverage_filepath = 'model/Food_Beverage_niche_resnet50_model_wo_opt.hdf5'
  21. lifestyle_travel_filepath = 'model/Lifestyle_Travel_niche_resnet50_model_wo_opt.hdf5'
  22. technology_filepath = 'model/Technology_niche_resnet50_model_wo_opt.hdf5'
  23.  
  24.  
  25. Fashion_classifier = tf.keras.models.load_model(fashion_filepath, custom_objects=None, compile=False)
  26. Beauty_Health_classifier = tf.keras.models.load_model(beauty_health_filepath, custom_objects=None, compile=False)
  27. Food_Beverage_classifier = tf.keras.models.load_model(food_beverage_filepath, custom_objects=None, compile=False)
  28. Lifestyle_Travel_classifier = tf.keras.models.load_model(lifestyle_travel_filepath, custom_objects=None, compile=False)
  29. Technology_classifier = tf.keras.models.load_model(technology_filepath, custom_objects=None, compile=False)
  30.  
  31.  
  32. def preprocess_image(image):
  33.  
  34. old_size = image.shape[:2] # old_size is in (height, width) format
  35.  
  36. ratio = float(IMG_SIZE)/max(old_size)
  37. new_size = tuple([int(x*ratio) for x in old_size])
  38.  
  39. # new_size should be in (width, height) format
  40.  
  41. image = cv2.resize(image, dsize = (new_size[1], new_size[0]), interpolation=cv2.INTER_NEAREST)
  42.  
  43. delta_w = IMG_SIZE - new_size[1]
  44. delta_h = IMG_SIZE - new_size[0]
  45. top, bottom = delta_h//2, delta_h-(delta_h//2)
  46. left, right = delta_w//2, delta_w-(delta_w//2)
  47.  
  48. color = [0, 0, 0]
  49. image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
  50. #image = cv2.resize(image, dsize=(IMG_SIZE, IMG_SIZE ), interpolation=cv2.INTER_NEAREST)
  51. image = (image/255.0) # normalize to [0,1] range
  52. return image
  53.  
  54.  
  55. def output(Status, fashion_score, beauty_health_score, food_beverage_score, lifestyle_travel_score, technology_score, images_flag):
  56. return {'Status': Status, 'Niche_score': {'Fashion': fashion_score, 'Beauty & Health': beauty_health_score, 'Food & Beverage': food_beverage_score,
  57. 'Lifestyle & Travel': lifestyle_travel_score, 'Technology': technology_score}, 'Pic_flag' : images_flag}
  58.  
  59.  
  60. def calculate_score(folderpath):
  61. user_im = []
  62. success_image = []
  63. image_files = os.listdir(folderpath)
  64. for image_path in image_files:
  65. try:
  66. image = plt.imread('{}/{}'.format(folderpath, image_path), format='jpeg')
  67. #image = file.read()
  68. #image = Image.open(io.BytesIO(image))
  69. #image = plt.imread(file, format='jpeg')
  70. user_im.append(preprocess_image(image))
  71. success_image.append(image_path)
  72. except:
  73. pass
  74.  
  75. if len(user_im) > 0:
  76. tensor_image = np.asanyarray(user_im)
  77. else:
  78. return output('Failed', 0, 0, 0, 0, 0, 'Empty')
  79.  
  80. fashion_proba_image = Fashion_classifier.predict(tensor_image)
  81. beauty_health_proba_image = Beauty_Health_classifier.predict(tensor_image)
  82. food_beverage_proba_image = Food_Beverage_classifier.predict(tensor_image)
  83. lifestyle_travel_proba_image = Lifestyle_Travel_classifier.predict(tensor_image)
  84. technology_proba_image = Technology_classifier.predict(tensor_image)
  85.  
  86. flag_fashion = np.argmax(fashion_proba_image, axis = 1)
  87. flag_beauty_health = np.argmax(beauty_health_proba_image, axis = 1)
  88. flag_food_beverage = np.argmax(food_beverage_proba_image, axis = 1)
  89. flag_lifestyle_travel = np.argmax(lifestyle_travel_proba_image, axis = 1)
  90. flag_technology = np.argmax(technology_proba_image, axis = 1)
  91.  
  92. images_flag = {}
  93.  
  94. for i in range(len(success_image)):
  95. value = []
  96. if flag_fashion[i]==1:
  97. value.append('Fashion')
  98. if flag_beauty_health[i]==1:
  99. value.append('Beauty and Health')
  100. if flag_food_beverage[i] == 1:
  101. value.append('Food and Beverage')
  102. if flag_lifestyle_travel[i] == 1:
  103. value.append('Lifestyle and Travel')
  104. if flag_technology[i] == 1:
  105. value.append('Technology')
  106.  
  107. images_flag[image_files[i]] = value
  108.  
  109. fashion_score = flag_fashion.sum()/len(fashion_proba_image)
  110. beauty_health_score = flag_beauty_health.sum()/len(beauty_health_proba_image)
  111. food_beverage_score = flag_food_beverage.sum()/len(food_beverage_proba_image)
  112. lifestyle_travel_score = flag_lifestyle_travel.sum()/len(lifestyle_travel_proba_image)
  113. technology_score = flag_technology.sum()/len(technology_proba_image)
  114.  
  115. return output('Success', fashion_score, beauty_health_score, food_beverage_score, lifestyle_travel_score, technology_score, images_flag)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement