def main(top_k):

    flower_arc = FlowerArc()
    saliency = SaliencyDetection()
    classifier = Classifier()
    st.title("Flower retrieval")
    train_img_fps, train_embs, train_labels = load_prec_embs()
    uploaded_file = st.file_uploader("Choose an image...")

    if uploaded_file is not None:
        st.image(uploaded_file,
                 caption='Uploaded Image.',
                 use_column_width=True)
        image = Image.open(uploaded_file)
        img_arr = np.array(image)

        flower_classifier = classifier.classification_predict(
            img_arr, consts.CLASSIFICATION_THRESHHOLD)

        if (flower_classifier == 0):
            st.subheader('Flower Detected')

            #saliency
            map_img = saliency.saliency_predict(img_arr)
            bounding_box_img = saliency.bounding_box(img_arr, map_img)
            st.image(bounding_box_img,
                     use_column_width=True,
                     caption='Flower Detection')
            # query emb
            test_emb = flower_arc.predict(img_arr)

            dists = cdist(test_emb, train_embs, metric='euclidean')[0]
            min_dist_indexes = dists.argsort()[:top_k]
            label_indexes = [
                train_labels[index] + 1 for index in min_dist_indexes
            ]
            img_fps = [train_img_fps[index] for index in min_dist_indexes]

            indices_on_page, images_on_page = \
                map(list, zip(*itertools.islice(zip(label_indexes, img_fps), 0, top_k)))  # noqa
            st.image(images_on_page, width=200, caption=indices_on_page)
        else:
            st.subheader(
                'Your image doesn\'t contain flowers or flowers are not clear enough to be detected, please try another image'
            )
예제 #2
0
def index():
    if request.method == 'POST':

        file = request.files['file']

        if file:
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            print(filename)
            # return url_for(filename)
            classifier = Classifier()
            result = classifier.classify(filename)
            print(result)
            return render_template('index.html',
                                   result=result,
                                   file=filename,
                                   form=False)
    return render_template('index.html', form=True)
예제 #3
0
파일: main.py 프로젝트: vanbasten17/NLP
class Main:
    dataset_dir = "dataset/"  # path to corpus

    print("Enter the number of frequent words to use:")
    N = int(input())

    print("Remove stopwords? [y/n]")
    flag_stopwords = str(input())

    classifier = Classifier(dataset_dir, int(N), flag_stopwords)
    classifier.most_frequent_words()
    classifier.compute_features()
    classifier.generate_arff()
예제 #4
0
def train():
    notcars = glob.glob('data/non-vehicles/*/*.png')
    cars = glob.glob('data/vehicles/*/*.png')

    print_stats(cars, notcars)

    features_car = []
    for car in cars:
        img = read_image(car)
        img_processed = process_image(img)
        features_car.append(extract_features(img_processed, parameters))

    features_notcar = []
    for notcar in notcars:
        img = read_image(notcar)
        img_processed = process_image(img)  # png
        features_notcar.append(extract_features(img_processed, parameters))

    features = np.vstack((features_car, features_notcar))
    # Fit a per-column scaler
    scaler = StandardScaler().fit(features)
    # Apply the scaler to X
    features_scaled = scaler.transform(features)
    # Define the labels vector
    labels = np.hstack(
        (np.ones(len(features_car)), np.zeros(len(features_notcar))))
    # Split up data into randomized training and test sets
    rand_state = np.random.randint(0, 100)
    out = train_test_split(features_scaled,
                           labels,
                           test_size=0.2,
                           random_state=rand_state)
    features_train, features_test, labels_train, labels_test = out

    # Initialize support vector machine object
    clf = SVC(kernel='linear', C=0.00001)
    # Check the training time for the SVC
    t = time.time()
    clf.fit(features_train, labels_train)
    print('{0:2.2f} seconds to train SVC...'.format(time.time() - t))

    # Accuracy score
    accuracy = clf.score(features_test, labels_test)
    print('Test Accuracy of SVC = {0:2.4f}'.format(accuracy))

    classifier = Classifier(clf, scaler)
    joblib.dump(classifier, 'classifier.pkl')

    return classifier
예제 #5
0
        if args.task_type == 'binary':
            if args.average:
                merged_preds[i] = avg_probs(prediction_dict[i])
            else:
                merged_preds[i] = merge_probs(prediction_dict[i], c)
        elif args.task_type == 'regression':
            merged_preds[i] = merge_regression(prediction_dict[i])
        elif args.task_type == 'multiclass':
            merged_preds[i] = avg_probs_multiclass(
                np.array(prediction_dict[i]))
    return merged_preds


for predictor_params in grid:
    print(predictor_params, flush=True)
    predictor = Classifier(**predictor_params).to(device)
    if n_gpu > 1:
        predictor = torch.nn.DataParallel(predictor)

    if not(args.freeze_bert) and not(args.use_adversary):
        param_optimizer = list(model.named_parameters()) + \
            list(predictor.named_parameters())
    elif args.freeze_bert and not(args.use_adversary):
        param_optimizer = list(predictor.named_parameters())
    elif args.freeze_bert and args.use_adversary:
        raise Exception(
            'No purpose in using an adversary if BERT layers are frozen')
    else:
        param_optimizer = list(model.named_parameters(
        )) + list(predictor.named_parameters()) + list(discriminator.named_parameters())
예제 #6
0
import sys
import time

import os
from practice import Load
from utils import Classifier
from utils import Data2Vec
from utils import Save
from utils.GlobalParam import *

if __name__ == "__main__":
    start_time = time.time()

    CLUSTER_STRUCTURE_NAME = CLUSTER_STRUCTURE_NAME + '_' + str(VEC_DIMENSION) + '_vec.bin'

    file_path = os.path.join(os.getcwd(), RESULT_DIRECTORY, CLUSTER_STRUCTURE_NAME)
    cluster_structure = Load.unpickling(file_path)

    print cluster_structure

    clf = Classifier.train_clf(cluster_structure)

    print clf.predict(Data2Vec.trim_data(sys.argv[1]))
    Save.bin2graph(sys.argv[1])

    end_time = time.time()

    print '*** TOATL TIME: ' + str(end_time - start_time) + ' ***'


예제 #7
0
				frame = imutils.rotate_bound(frame, 90)
			if ret:
				# Return a boolean success flag and the current frame converted to BGR
				return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
			else:
				return (ret, None)
		else:
			return (ret, None)

	# Release the video source when the object is destroyed
	def __del__(self):
		if self.vid.isOpened():
			self.vid.release()

#When open the app, load retina model first
global retina_model, classicifer_model, EAST
print("[INFO] Loading the retina_model ...")
retina_model = Retina()
print("[INFO] Loading the retina_model completed")
print("[INFO] Loading the classifier_model ...")
classicifer_model = Classifier()
print("[INFO] Loading the classifier_model completed")

layerNames = ["feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"]
EASTPATH = os.path.join("model", "EAST.pb")
print("[INFO] Loading the pre-trained EAST text detector ...")
EAST = cv2.dnn.readNet(EASTPATH)
print("[INFO] Loading the pre-trained EAST text completed")

# Create a window and pass it to the Application object
App(tkinter.Tk(), "Invoice lottery (Tkinter and OpenCV support)")
예제 #8
0
def manual_classes_classifier(data_path, column, language, lemmatize,
                              manual_mappings, manual_classes,
                              predicted_classes_filename, should_upload_db,
                              account_key_path):
    print("Build classifier...")
    with open(manual_classes, encoding="utf8") as json_data:
        manual_classes_dict = json.load(json_data)
    classifier = Classifier(manual_classes_dict, language)
    print("Classifier built")
    print()

    print("Loading data...")
    data_df = load_data(data_path, column)
    print("Loaded data sample")
    print(data_df.head())
    print()

    print("Cleaning data...")
    data_df[column] = clean_data(data_df[column])
    print("Clean data sample")
    print(data_df.head())
    print()

    print("Removing stopwors...")
    data_df[column] = remove_stopwords(data_df[column], language)
    print("Data sample")
    print(data_df.head())
    print()

    if lemmatize:
        print("Lemmatizing data...")
        data_df[column] = lemmatize_text(data_df[column], language)
        print("Lemmatized data sample")
        print(data_df.head())
        print()

    if manual_mappings:
        print("Applying manual mappings...")
        data_df[column] = apply_manual_mappings(data_df[column],
                                                manual_mappings)
        print("Manually mapped data sample")
        print(data_df.head())
        print()

    print("Predict classes...")
    predicted_classes = predict(classifier, data_df[column])
    save_classes(predicted_classes, predicted_classes_filename)
    print("Predicted classes saved to:", predicted_classes_filename)
    print()

    if should_upload_db:
        db_client = connect_db(account_key_path)
        print("Uploading predicted classes to db...")
        upload_db(
            db_client, 'predicted_classes', {
                column:
                json.loads(
                    pd.DataFrame(predicted_classes).to_json(orient='index',
                                                            force_ascii=False))
            })
        print('Done')
        print()
예제 #9
0
        # make iterator
        train_iter = chainer.iterators.SerialIterator(
            train_datasets, batch_size)
        val_iter = chainer.iterators.SerialIterator(
            val_datasets, batch_size, repeat=False, shuffle=False
        )
        test_iter = chainer.iterators.SerialIterator(
            test_datasets, batch_size, repeat=False, shuffle=False)

        # build model
        model = chainer.links.VGG16Layers()
        num_ftrs = model.fc8.out_size
        # model.fc8 = L.Linear(in_size=None, out_size=101,
        #                      initialW=initializers.Normal(scale=0.02),
        #                      initial_bias=initializers.Normal(scale=0.02))
        model = Classifier(model)
        # model = L.Classifier(model)

        if gpu >= 0:
            # Make a specified GPU current
            chainer.backends.cuda.get_device_from_id(gpu).use()
            model.to_gpu()  # Copy the model to the GPU

        # make optimizer
        optimizer = optimizers.MomentumSGD(lr=0.001, momentum=0.9)
        optimizer.setup(model)

        # make updater & set up trainer
        updater = updaters.StandardUpdater(
            train_iter, optimizer, device=gpu)
        trainer = training.Trainer(
parser.add_argument("--num_lstm_layers", type=int, default=2)
parser.add_argument("--dropout_prob", type=float, default=0.1)

if __name__ == '__main__':

    args = parser.parse_args()

    device = torch.device("cpu")
    if torch.cuda.is_available():
        device = torch.device("cuda")

    dl = DataLoader(batch_size=32)
    test_data = dl.setup(return_only_test_data=True)
    test_data = dl.test_dataloader(test_data)

    wts = torch.load(args.weights_path, map_location=torch.device("cpu"))
    model = Classifier(dl.vocab_size, args.embedding_size,
                       args.num_lstm_layers, args.dropout_prob)
    model.load_state_dict(wts)
    model.to(device)

    preds, labels = model.predict(test_data, device)

    cm = confusion_matrix(labels, preds)
    names = [
        "negative", "somewhat negative", "neutral", "somewhat positive",
        "positive"
    ]
    cm = pd.DataFrame(cm, columns=names, index=names)
    print(cm)
예제 #11
0
    #Pre-trainned network load
    
    if arch == 'alexnet':
        model = models.alexnet(pretrained=True)
        input_size = 9216                        #Based on pretrained model alexnet in_features value
    elif arch == 'vgg19':
        model = models.vgg19(pretrained=True)
        input_size = 25088                       #Based on pretrained model vgg19 in_features value
    else:
        model = models.densenet121(pretrained=True)
        input_size = 1024                        #Based on pretrained model desenet121 in_features value
   
    #Classifier creation
 
    output_size = 102            # According with the 102 flower classes
    classifier = Classifier(input_size, hidden_layers, output_size, drop_p)
    
    print('\n')
    print('---'*20)
    print('                  Trainning CNN Parameters ')
    print('---'*20)
    print(' Data directory: {:12}        Checkpoint:    {:12}'.format(color(data_dir), color(save_dir)))
    print(' CNN Pretrained: {:12}    Learning rate: {:12} Epochs: {:12}'.format(color(arch), color(lr), color(epochs)))
    print(' Hidden Units:   {:12}    Dropout:       {:12}   GPU:    {:12}'.format(color(hidden_layers), color(drop_p), color(gpu)))
    print('---'*20)
    print(color(' Classifier'), classifier)
    print('---'*20)

    #Freeze params for the pretrainned model and avoid back propagation

    for param in model.parameters():
예제 #12
0
def main():

    m = Mask()
    c = Classifier()

    device = torch.device('cuda')

    # IR_50
    model_ir50 = IR_50([112, 112])
    model_ir50.load_state_dict(
        torch.load('./ckpt/backbone_ir50_ms1m_epoch120.pth',
                   map_location='cuda'))
    model_ir50.eval().to(device).zero_grad()

    # IR_152
    model_ir152 = IR_152([112, 112])
    model_ir152.load_state_dict(
        torch.load('./ckpt/Backbone_IR_152_Epoch_112_Batch.pth',
                   map_location='cuda'))
    model_ir152.eval().to(device).zero_grad()

    # IR_SE_50
    model_irse50 = Backbone(50, mode='ir_se')
    model_irse50.load_state_dict(
        torch.load('./ckpt/model_ir_se50.pth', map_location='cuda'))
    model_irse50.eval().to(device).zero_grad()

    eps = (args.max_epsilon / 255.0)
    alpha = eps / args.iterations

    momentum = args.momentum

    kernel = gkern(args.kernlen, args.sig).astype(np.float32)
    stack_kernel = np.stack([kernel, kernel, kernel])
    stack_kernel = np.expand_dims(stack_kernel, 1)
    stack_kernel = torch.Tensor(stack_kernel).to(device)

    counter = 0
    total_distance = 0
    num = 1
    for raw_images, filenames, _ in load_images_with_names(
            args.input_dir, args.batch_size):

        if num * args.batch_size > 712:
            batch_size = 712 - (num - 1) * args.batch_size
        else:
            batch_size = args.batch_size
        num += 1

        in_tensor = process(raw_images)
        raw_variable = in_tensor.detach().to(device)

        # raw embedding
        raw_ir50 = model_ir50(raw_variable)
        raw_ir152 = model_ir152(raw_variable)
        raw_irse50 = model_irse50(raw_variable)

        true_labels = c.classifier(raw_ir50.data.cpu().detach().numpy())

        bias_ir50, bias_ir152, bias_irse50 = found_bias_v2(
            raw_ir50.data.cpu().detach().numpy(),
            raw_ir152.data.cpu().detach().numpy(),
            raw_irse50.data.cpu().detach().numpy(), batch_size)

        perturbation = torch.Tensor(batch_size, 3, 112,
                                    112).uniform_(-0.01, 0.01).to(device)
        in_variable = raw_variable + perturbation
        in_variable.data.clamp_(-1.0, 1.0)
        in_variable.requires_grad = True

        last_grad = 0.0
        momentum_sum = 0.0

        for step in range(args.iterations):

            new_ir50 = model_ir50(in_variable)
            new_ir152 = model_ir152(in_variable)
            new_irse50 = model_irse50(in_variable)

            loss1 = -torch.mean(
                torch.cosine_similarity(x1=raw_ir50, x2=new_ir50, dim=1) * 1.7
                + torch.cosine_similarity(x1=raw_ir152, x2=new_ir152, dim=1) *
                0.35 +
                torch.cosine_similarity(x1=raw_irse50, x2=new_irse50, dim=1) *
                0.65) / 2.7

            loss2 = torch.mean(
                torch.cosine_similarity(
                    x1=torch.from_numpy(bias_ir50).detach().to(device),
                    x2=new_ir50,
                    dim=1) * 1.7 + torch.cosine_similarity(x1=torch.from_numpy(
                        bias_ir152).detach().to(device),
                                                           x2=new_ir152,
                                                           dim=1) * 0.35 +
                torch.cosine_similarity(x1=torch.from_numpy(
                    bias_irse50).detach().to(device),
                                        x2=new_irse50,
                                        dim=1) * 0.65) / 2.7
            loss = loss1 + loss2

            print('loss :', loss)

            loss.backward(retain_graph=True)

            data_grad = in_variable.grad.data

            data_grad = F.conv2d(data_grad,
                                 stack_kernel,
                                 padding=(args.kernlen - 1) // 2,
                                 groups=3)

            for i in range(data_grad.shape[0]):
                data_grad[i] = data_grad[i] / torch.mean(
                    data_grad[i].norm(2, 0) / 1.713)

            if iter == 0:
                noise = data_grad
            else:
                noise = last_grad * momentum + data_grad * 0.9

            last_grad = noise.detach()
            norm = noise.norm(dim=1).unsqueeze(1)
            index = norm.mean()
            momentum_sum = momentum_sum * momentum + 1.0
            d_img = noise * norm * alpha / (momentum_sum * index)
            d_img = d_img / d_img.norm(dim=1).mean() * alpha

            perturb_mask = m.get_perturb_mask(
                new_ir50.data.detach().cpu().numpy(),
                new_ir152.data.detach().cpu().numpy(),
                new_irse50.data.detach().cpu().numpy(), true_labels,
                args.cos_margin)

            in_variable.data = in_variable.data + \
                               d_img * torch.from_numpy(perturb_mask.reshape([batch_size, 1, 1, 1])).to(device).float()

            raw_variable.data = torch.clamp(in_variable.data, -1.0, 1.0)
            in_variable.grad.data.zero_()

        advs = raw_variable.data.cpu().detach().numpy()
        advs = advs.swapaxes(1, 2).swapaxes(2, 3)

        total_distance_ = save_images(raw_images, advs, filenames,
                                      args.output_dir)
        total_distance += total_distance_
        counter += batch_size
        print('attack images num : [%d / 712]' % counter)
    print('mean_dist:', total_distance / 712.0)