def delete_entry(str_id): # For return path id = ObjectId(str_id) sample = db.get_sample_by_id(id) if sample is None: set_error('Item to delete not found.') return redirect('/') dataset_id = sample['dataset_id'] readonly = db.is_readonly_dataset_id(dataset_id) sample_index, sample_count, prev_sample_id, next_sample_id = db.get_sample_index(dataset_id, id) if readonly: set_error('Dataset is protected.') elif db.delete_sample(id): set_notice('Item deleted.') else: set_error('Could not delete item.') # Redirect: To next sample in set if it exists. Otherwise to the dataset page. if next_sample_id is not None and next_sample_id != id: # Return to same kind of page (info/annotation/diff_annotation) is_annotation = request.args.get('annotate') is_differential = request.args.get('differential') if is_annotation: suffix = '?differential=1' if is_differential else '' return redirect('/annotate/' + str(next_sample_id) + suffix) else: return redirect('/info/' + str(next_sample_id)) else: return redirect('/dataset/' + str(dataset_id))
def upload_archive(dataset_id, file, _filename, is_new_dataset): zipf = ZipFile(file.stream, 'r') print 'ZIPF:' for nfo in zipf.infolist(): if nfo.file_size > config.max_image_file_size: return error_redirect( 'One of the images (%s) exceeds the max file size (%d).' % (nfo.filename, config.max_image_file_size)) n_added = 0 for nfo in zipf.infolist(): # Check image_filename = secure_filename(nfo.filename) basename, ext = os.path.splitext(image_filename) if ext not in config.image_extensions: print 'Not an image: %s (%s)' % (ext, image_filename) continue # Extract full_fn = make_unique_server_image_filename(image_filename) zfile = zipf.open(nfo) with open(full_fn, 'wb') as fid: contents = zfile.read() fid.write(contents) print 'Written to ', full_fn # Add if add_uploaded_image(dataset_id, full_fn, image_filename) is not None: n_added += 1 else: # Cleanup invalid if os.path.isfile(full_fn): os.remove(full_fn) set_notice('%d images added.' % n_added) ds_url_suffix = '?new=true' if is_new_dataset else '' return redirect('/dataset/%s' % str(dataset_id) + ds_url_suffix)
def enqueue_all_validation_sets(): n = 0 m = 0 for model in db.get_models(details=False, status=db.model_status_trained): nm = enqueue_validation_sets_for_model(model['_id']) if nm: n += nm m += 1 set_notice('Enqueued %d sets from %d models.' % (n, m)) return redirect('/admin')
def delete_dataset(dataset_id_str): dataset_id = ObjectId(dataset_id_str) dataset_info = db.get_dataset_by_id(dataset_id) if dataset_info is None: return render_template("404.html") if db.is_readonly_dataset(dataset_info): set_error('Dataset is protected.') return redirect('/dataset/' + str(dataset_info['_id'])) db.delete_dataset(dataset_id) set_notice('Dataset "%s" deleted.' % dataset_info['name']) return redirect('/')
def enqueue_all_images(): n = 0 m = 0 for model in db.get_models(details=False, status=db.model_status_trained): if not model['primary']: nm = enqueue_images_for_model(model['_id']) if nm: n += nm m += 1 set_notice('Enqueued %d images from %d models.' % (n, m)) return redirect('/admin')
def dataset_set_threshold(dataset_id_str, new_threshold_str): dataset_id = ObjectId(dataset_id_str) dataset_info = db.get_dataset_by_id(dataset_id) new_threshold = float(new_threshold_str) if dataset_info.get('threshold_prob') == new_threshold: set_notice('Threshold not updated: Values are identical.') else: db.set_dataset_threshold_prob(dataset_id, new_threshold) count = db.remove_machine_annotations_for_dataset(dataset_id) set_notice('Threshold updated. %d annotations removed.' % count) return redirect('/dataset/' + dataset_id_str)
def admin_retrain(): data = request.form model_name = data['train_model_name'] # Race condition here; but it's just for the admin page anyway if re.match('^[\w-]+$', model_name) is None: set_error( 'Invalid model name. Must be non-empty, only alphanumeric characters, dashes and underscores.' ) return redirect('/admin') if db.get_model_by_name(model_name): set_error('Model exists. Please pick a different name.') return redirect('/admin') # Validate tag tag_name = data['train_label'] dsets = list(db.get_datasets_by_tag(tag_name)) if not len(dsets): set_error('Train tag does not match any datasets.') return redirect('/admin') is_primary = (data.get('train_primary') == 'on') dataset_only = (data.get('dataset_only') == 'on') train_sample_limit_s = data['train_sample_limit'] if len(train_sample_limit_s): try: train_sample_limit = int(train_sample_limit_s) if train_sample_limit <= 0: raise ValueError() except ValueError as e: set_error('Invalid sample limit. Either leave empty for no limit, ' 'or supply a valid number greater than zero.') return redirect('/admin') else: train_sample_limit = None # Add scheduled model training to DB. Will be picked up by worker. rec = db.add_model(model_name=model_name, margin=96, sample_limit=train_sample_limit, train_tag=tag_name, scheduled_primary=is_primary, status=db.model_status_scheduled, dataset_only=dataset_only) set_notice('Model training scheduled.') return redirect('/model/' + str(rec['_id']))
def dataset_rerun(dataset_id_str): dataset_id = ObjectId(dataset_id_str) count = db.remove_machine_annotations_for_dataset(dataset_id) set_notice('%d annotations removed.' % count) return redirect('/dataset/' + dataset_id_str)
def delete_expired_datasets(): ds = find_old_datasets() delete_datasets(ds) set_notice('%d datasets deleted' % len(ds)) return redirect('/admin')
def enqueue_validation_sets(model_id_s): model_id = ObjectId(model_id_s) n = enqueue_validation_sets_for_model(model_id) set_notice('Enqueued %d sets.' % n) return redirect('/model/' + model_id_s)
def set_primary_model(model_id_s): model_id = ObjectId(model_id_s) db.set_primary_model(model_id) set_notice('Updated primary model.') return redirect('/model/' + model_id_s)
def enqueue_images(model_id_s): model_id = ObjectId(model_id_s) n = enqueue_images_for_model(model_id) set_notice('Enqueued %d images.' % n) return redirect('/model/' + model_id_s)