Advertisement
Guest User

Script

a guest
Oct 31st, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. import csv
  2. import os
  3. import random
  4. # import paramiko
  5. # import pandas as pd
  6. # import scipy as sp
  7. # import pipes
  8. # import subprocess
  9. from itertools import groupby
  10. from shutil import copyfile
  11. # from sklearn.cross_validation import train_test_split
  12.  
  13.  
  14. def read_images(filename=None):
  15.     """
  16.    Reading list of images from file
  17.    :param filename: name of csv file, that contains path to files
  18.    :return: list with elements following format : [image_name, image_path, microsegment, prob]
  19.    """
  20.     images_list = []
  21.     with open(filename, 'rb') as file:
  22.         reader = csv.reader(file)
  23.         for row in reader:
  24.             if len(images_list) > 5000:
  25.                 break
  26.             plate, link, microsegment, prob = row
  27.             image_name = link.split('/')[-1]
  28.             image_path = '/'.join(link.split('/')[-2:])
  29.             car = [image_name, image_path, microsegment, prob]
  30.             if ".jpg" in image_name  and len(link) != 0 and os.path.isfile("/home/synaps/images/{}".format(image_path)):
  31.                 print "Image exists and added"
  32.                 images_list.append(car)
  33.     return images_list
  34.  
  35.  
  36. def file_exists(path):
  37.     local_path = "/home/synaps/images/{}".format(path)
  38.     client = paramiko.SSHClient()
  39.     client.load_system_host_keys()
  40.     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  41.     client.connect("81.211.114.194", username="aleksandr", password="P@ssw0rd", port=8822)
  42.     _, stdout, _ = client.exec_command("[ -f {} ] && echo OK".format(local_path))
  43.     if "OK" in stdout.read():
  44.         return True
  45.     return False
  46.  
  47.  
  48. def select_images(img_list):
  49.     """
  50.    Select images which have enough volume in population
  51.    :param img_list: list of images to select
  52.    :return: list of selected images
  53.    """
  54.     """
  55.    limit = 5000
  56.    res = []
  57.    for k, g in groupby(images_list, lambda x: x[2]):
  58.        if len(res) >= limit:
  59.            break
  60.        m = list(g)
  61.        if len(m) > 4:
  62.            for e in m:
  63.                res.append(e)
  64.    return res
  65.  
  66.  
  67. def save_images(data, y, folder_name):
  68.    folder_path = "./datasets/alexander/data/{}/".format(folder_name)
  69.    storage_path = "/home/synaps/images/{}"
  70.    file = open("./datasets/alexander/data/" + folder_name + ".txt", "w")
  71.    for img,l in zip(data, y):
  72.        file.write(img.split('/')[-1] + " " + l+"\n")
  73.    file.close()
  74.    for img in data:
  75.        img_path = storage_path.format(img)
  76.        print img_path
  77.        copyfile(img_path, folder_path + img_path.split('/')[-1])
  78.  
  79.  
  80. def cross_validation(data, test_size=0.25):
  81.    X, y = [], []
  82.    random.shuffle(data)
  83.    n = len(data)
  84.    m = int((1 - test_size) * n)
  85.    for car in data:
  86.        X.append(car[1])
  87.        y.append(car[2])
  88.    X_learn, X_validate, y_learn, y_validate = X[:m], X[m:], y[:m], y[m:]
  89.    save_images(data=X_learn,y=y_learn, folder_name="train")
  90.    save_images(data=X_validate,y=y_validate, folder_name="val")
  91.  
  92.  
  93. images_list = read_images(filename='Microsegments_data.csv')
  94. print len(images_list)
  95. selected_imgs = select_images(images_list)
  96. print len(selected_imgs)
  97. print selected_imgs[0]
  98. cross_validation(selected_imgs)
  99.  
  100. # !scripts/create_net.sh utkin
  101. # !scripts/make_net_mean.sh utkin
  102. # !$CAFFE_ROOT/build/tools/caffe train --solver=models/bvlc_reference_caffenet_utkin/solver.prototxt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement