def check_features(data_type, network='Resnet50'):
    """Check if features file exists locally
       Only one dataset, e.g. train, valid, test is checked
    """
    data_dir = bfeatures.set_features_file(data_type, network,
                                           return_type='dir')
    bottleneck_file =  bfeatures.set_features_file(data_type,
                                                   network, return_type='file')
    bottleneck_path = bfeatures.set_features_file(data_type, network)
    bottleneck_exists, _ = dutils.maybe_download_data(data_dir=data_dir,
                                                      data_file=bottleneck_file)

    if not bottleneck_exists:
        print("[INFO] %s was neither found nor downloaded. Trying to build. It may take time .. "
              % bottleneck_file)

        # check if directory with train, test, and valid images exists:
        dutils.maybe_download_and_unzip()
        bfeatures.build_features(data_type, network)
        
        # Upload to nextcloud newly created file
        bottleneck_exists = True if os.path.exists(bottleneck_path) else False
        dest_dir = cfg.Dog_RemoteStorage.rstrip('/') + '/'+ data_dir
        print("[INFO] Upload %s to %s" % (bottleneck_path, dest_dir))
        dutils.rclone_copy(bottleneck_path, dest_dir)
        
    return bottleneck_exists
def prepare_data(network='Resnet50'):
    """ Function to prepare data
    """
   
    # check if dog_names file exists locally, if not -> download,
    # if not downloaded -> dutils.dog_names_create()
    dog_names_file = cfg.Dog_LabelsFile.split('/')[-1]
    status_dog_names, _ = dutils.maybe_download_data(data_dir='/data',
                                                     data_file=dog_names_file)

    if not status_dog_names:
        dutils.maybe_download_and_unzip()
        dutils.dog_names_create()
    else:
        print("[INFO] %s exists" % (cfg.Dog_LabelsFile))

    # check if bottleneck_features file exists locally
    # if not -> download it, if not downloaded -> try to build
    # train
    status = { True: "exists", False: "does not exist"}
    datasets = ['train', 'valid', 'test']
    for dset in datasets:
        status_targets = check_targets(dset)
        print("[INFO] Targets file for %s %s" % (dset, status[status_targets]))
        status_bottleneck = check_features(dset, network)
        print("[INFO] Bottleneck file for %s (%s) %s" % 
               (dset, network, status[status_bottleneck]))
def check_targets(data_type):
    """Check if targets file exists locally
       Only one dataset, e.g. train, valid, test is checked
    """
    data_dir = '/data'
    targets_file = 'Dogs_targets_' + data_type + '.npz'
    targets_path = os.path.join(cfg.BASE_DIR, 'data', targets_file)
    targets_exists, _ = dutils.maybe_download_data(data_dir=data_dir,
                                                   data_file=targets_file)

    if not targets_exists:
        print("[INFO] %s was neither found nor downloaded. Trying to build .. "
              % targets_file)

        # check if directory with train, test, and valid images exists:
        dutils.maybe_download_and_unzip()
        dutils.build_targets(data_type)

        # Upload to nextcloud newly created file
        targets_exists = True if os.path.exists(targets_path) else False
        dest_dir = cfg.Dog_RemoteStorage.rstrip('/') + data_dir
        print("[INFO] Upload %s to %s" % (targets_path, dest_dir))    
        dutils.rclone_copy(targets_path, dest_dir)

    return targets_exists
Exemple #4
0
def predict_file(img_path, network='Resnet50'):
    """
    Function to make prediction which breed is closest
    :param img_path: image to classify, full path
    :param network: neural network to be used    
    :return: most probable dogs breed
    """

    nets = {
        'VGG16': bfeatures.extract_VGG16,
        'VGG19': bfeatures.extract_VGG19,
        'Resnet50': bfeatures.extract_Resnet50,
        'InceptionV3': bfeatures.extract_InceptionV3,
    }

    # clear possible pre-existing sessions. IMPORTANT!
    K.clear_session()

    # check that all necessary data is there
    #prepare_data(network)

    weights_file = mutils.build_weights_filename(network)
    saved_weights_path = os.path.join(cfg.BASE_DIR, 'models', weights_file)

    # check if the weights file exists locally. if not -> try to download
    status_weights, _ = dutils.maybe_download_data(data_dir='/models',
                                                   data_file=weights_file)

    # attempt to download default weights file
    if not status_weights and network in cfg.cnn_list:
        print("[INFO] Trying to download weights from the public link")
        url_weights = cfg.Dog_RemoteShare + weights_file
        status_weights, _ = dutils.url_download(url_path=url_weights,
                                                data_dir=os.path.join(
                                                    cfg.BASE_DIR, 'models'),
                                                data_file=weights_file)

    dog_names_file = cfg.Dog_LabelsFile.split('/')[-1]
    # check if the labels file exists locally. if not -> try to download
    status_dog_names, _ = dutils.maybe_download_data(data_dir='/data',
                                                     data_file=dog_names_file)

    # attempt to download labels file
    if not status_dog_names:
        print("[INFO] Trying to download labels from the public link")
        url_dog_names = cfg.Dog_RemoteShare + dog_names_file
        status_weights, _ = dutils.url_download(url_path=url_dog_names,
                                                data_dir=os.path.join(
                                                    cfg.BASE_DIR, 'data'),
                                                data_file=dog_names_file)

    if status_weights:
        dog_names = dutils.dog_names_load()
        net_model = build_model(network, len(dog_names))
        net_model.load_weights(saved_weights_path)

        # extract bottleneck features
        bottleneck_feature = nets[network](dutils.path_to_tensor(img_path))
        if debug_model:
            print("[INFO] Bottleneck feature size: {}".format(
                bottleneck_feature.shape))

        # obtain predicted vector
        predicted_vector = net_model.predict(bottleneck_feature)
        print("[INFO] Sum: %f" % np.sum(predicted_vector))
        # return dog breed that is predicted by the model
        idxs = np.argsort(predicted_vector[0])[::-1][:5]
        # dog_names_best = [ dog_names[i] for i in idxs ]
        dog_names_best = []
        probs_best = []
        for i in idxs:
            dog_names_best.append(dog_names[i])
            probs_best.append(predicted_vector[0][i])
            print("%s : %f" % (dog_names[i], predicted_vector[0][i]))

        msg = mutils.format_prediction(dog_names_best, probs_best)
    else:
        msg = "ERROR in predict_file(). No weights file found! " + \
              "Please first train the model with the " + network + " network!"
        msg = {"Error": msg}

    return msg