### Developer Installation of PaQ-2-PiQ Source: https://github.com/baidut/paq-2-piq/blob/master/README.md Recommended for developers. This involves cloning the repository and installing all dependencies listed in requirements.txt. This setup allows for easier code modification and contribution. ```bash git clone https://github.com/baidut/PaQ-2-PiQ cd PaQ-2-PiQ pip install -r requirements.txt ``` -------------------------------- ### Setup and Train FastIQA Models Source: https://github.com/baidut/paq-2-piq/wiki/Home This snippet demonstrates how to set up an experiment, load a dataset, add different ResNet models as learners, and start training. It also shows how to validate the trained models on other databases. ```python from fastiqa.basics import * # setup an experiment on gpu 0 e = IqaExp('test_different_models', gpu=0) # pick a dataset: CLIVE data, Im2MOS format: input image, output MOS data = Im2MOS(CLIVE, batch_size=16) # add learners for model in [models.resnet18, models.resnet34, models.resnet50]: e += iqa_cnn_learner(data, model) # start training all models e.fit(10) # validate on other databases e.valid(on=[Im2MOS(KonIQ), Im2MOS(FLIVE)]) ``` -------------------------------- ### Install PaQ-2-PiQ via PyPI Source: https://github.com/baidut/paq-2-piq/blob/master/README.md Use this command to install the latest stable version of the library from the Python Package Index. This method automatically installs compatible versions of PyTorch and FastAI. ```bash pip install fastiqa ``` -------------------------------- ### Setup and Train IQA Experiment Source: https://github.com/baidut/paq-2-piq/blob/master/README.md Sets up an IQA experiment, loads data, adds various CNN learners, trains them, and validates on different databases. Requires `fastiqa` library and PyTorch. ```python %matplotlib inline from fastiqa.basics import * # setup an experiment on gpu 0 e = IqaExp('test_different_models', gpu=0) # pick a dataset: CLIVE data, Im2MOS format: input image, output MOS data = Im2MOS(CLIVE, batch_size=16) # add learners for model in [models.resnet18, models.resnet34, models.resnet50]: e += iqa_cnn_learner(data, model) # start training all models e.fit(10) # validate on other databases e.valid(on=[Im2MOS(KonIQ), Im2MOS(FLIVE)]) ``` -------------------------------- ### Install Latest Development Version from GitHub Source: https://github.com/baidut/paq-2-piq/blob/master/README.md Install the most recent version directly from the GitHub repository to get the latest bug fixes. This is useful if you need features or fixes not yet released on PyPI. ```bash pip install git+https://github.com/baidut/PaQ-2-PiQ.git ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/baidut/paq-2-piq/blob/master/demo.ipynb Install all the necessary Python packages listed in the 'requirements.txt' file. This command ensures all dependencies for the project are met. ```bash !pip install -r requirements.txt ``` -------------------------------- ### Clone PaQ-2-PiQ Repository Source: https://github.com/baidut/paq-2-piq/blob/master/demo.ipynb Use this command to clone the PaQ-2-PiQ repository from GitHub. This is the first step to get the project files. ```bash !git clone https://github.com/baidut/PaQ-2-PiQ ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Imports all required libraries for data manipulation, file operations, and network requests. Ensure these are installed before running. ```python import os import tarfile import shutil import glob import requests import zipfile import io import urllib import cv2 import pandas as pd import sys from sys import stdout ``` -------------------------------- ### Define Custom Model Architecture Source: https://github.com/baidut/paq-2-piq/wiki/Home Define a custom model by extending IqaModel. This example uses a pre-trained ResNet18 backbone and a custom head, demonstrating how to split the model for differential learning rates and define a bunching method. ```python class BodyHeadModel(IqaModel): def __init__(self): super().__init__() self.__name__ = self.__class__.__name__ self.body = create_body(models.resnet18) nf = num_features_model(self.body) * 2 self.head = create_head(nf, 1) def forward(self, img): feat = self.body(img) pred = self.head(feat) return pred @staticmethod def split_on(m): return [[m.body], [m.head]] @staticmethod def bunch(self, label, **kwargs): return Im2MOS(label, **kwargs) ``` -------------------------------- ### Basic IQA Model Training Source: https://github.com/baidut/paq-2-piq/blob/master/fastiqa/README.md This snippet demonstrates the fundamental steps for training an IQA model using FastIQA. It initializes a dataset, defines a model architecture, creates an IqaLearner, and initiates training. It also shows how to validate the trained model on different datasets. ```python %matplotlib inline from fastiqa.basics import * label = CLIVE # LIVE Challenge Database data = Im2MOS(label, valid_pct=0.3) model = BodyHeadModel(backbone=resnet18) learn = IqaLearner(data, model) learn.fit(10) # training for 10 epochs learn.valid(on=[CLIVE, All(KonIQ), All(FLIVE)]) # valid on other databases ``` -------------------------------- ### Initialize Image Testing Source: https://github.com/baidut/paq-2-piq/blob/master/demo.ipynb Use this snippet to set up the image testing process. Specify the input image folder and the output directory for quality maps. Scores are automatically saved to 'scores.csv'. ```python label = TestImages.from_learner(learn, path='images', dir_qmap='!qmap') ``` -------------------------------- ### Visualizing Model Performance Source: https://github.com/baidut/paq-2-piq/blob/master/fastiqa/README.md This snippet shows how to visualize the performance of a trained IQA model using a joint plot. It requires a pre-trained learner and a specified dataset for visualization. ```python from fastqia.vis import * learn.jointplot(on=KonIQ) ``` -------------------------------- ### Load Model and Predict Quality Map Source: https://github.com/baidut/paq-2-piq/blob/master/demo.ipynb This snippet shows how to load a pre-trained RoIPoolModel and predict a quality map for a given image. Ensure you have the necessary data and model files. ```python %matplotlib inline from fastiqa.all import * file = 'images/Picture1.jpg' data = Im2MOS(TestImages) model = RoIPoolModel() learn = RoIPoolLearner(data, model, path='.') learn.load('RoIPoolModel') im = open_image(file) qmap = learn.predict_quality_map(im, [5, 5]) qmap ``` -------------------------------- ### IQA Model Ablation Study Source: https://github.com/baidut/paq-2-piq/blob/master/fastiqa/README.md This snippet illustrates how to conduct an ablation study to compare different backbone architectures for an IQA model. It sets up an experiment, iterates through various backbones, trains models, and validates their performance across multiple datasets. ```python from fastiqa.basics import * e = IqaExp('different_backbone') data = Im2MOS(CLIVE) for backbone in [alexnet, resnet18, resnet34]: model = BodyHeadModel(backbone=backbone) e[backbone.__name__] = IqaLearner(data, model) e.fit(10) e.valid(on=[CLIVE, All(KonIQ), All(FLIVE)]) ``` -------------------------------- ### Train Model with IqaLearner Source: https://github.com/baidut/paq-2-piq/wiki/Home Use `IqaLearner` when you have an instantiated model object (e.g., `models.resnet18()`). This learner is similar to Fastai's `IqaLearner` but includes specific functions for IQA. Prepare the `Im2MOS` data object beforehand. ```python # iqa_cnn_learner is just like IqaLearner in Fastai but with useful functins data = Im2MOS(KonIQ) model = models.resnet18() learn = IqaLearner(data, model) learn.fit(10) ``` -------------------------------- ### Download and Extract Blur Detection Dataset Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Downloads the Blur Detection Dataset from its URL, extracts the zip file, and moves all images to the 'database/blur_dataset' directory, removing the intermediate 'image' folder. ```python url_blur = "http://www.cse.cuhk.edu.hk/~leojia/projects/dblurdetect/data/BlurDatasetImage.zip" blur, headers = urllib.request.urlretrieve(url_blur, 'blur.zip') zipfile.ZipFile('blur.zip','r').extractall('database/blur_dataset') for path in glob.glob('database/blur_dataset/image/*'): shutil.move(path, 'database/blur_dataset/') shutil.rmtree('database/blur_dataset/image') ``` -------------------------------- ### Download RoIPoolModel Source: https://github.com/baidut/paq-2-piq/blob/master/demo.ipynb This snippet downloads the pre-trained RoIPoolModel.pth file and saves it into the 'models' directory. Ensure the 'models' directory exists before running. ```python !mkdir models !wget -O models/RoIPoolModel.pth -N https://github.com/baidut/PaQ-2-PiQ/releases/download/v1.0/RoIPoolModel-fit.10.bs.120.pth ``` -------------------------------- ### Change Working Directory Source: https://github.com/baidut/paq-2-piq/blob/master/demo.ipynb After cloning, change the current working directory to the newly cloned 'PaQ-2-PiQ' folder. This ensures subsequent commands are executed within the project context. ```python import os os.chdir('PaQ-2-PiQ/') ``` -------------------------------- ### Create Patches Directory Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Ensures the 'database/patches' directory exists. Handles potential OS errors during directory creation. ```python try: if os.path.isdir('database/patches') == False: os.mkdir('database/patches') except OSError: print ("Creation of the directory %s failed" % 'patch') ``` -------------------------------- ### List Database Folders Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Retrieves a list of all subdirectories within the 'database' folder. ```python folders = os.listdir('database') ``` -------------------------------- ### Download VOC 2012 Dataset Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Downloads the VOC 2012 dataset archive from the specified URL and saves it as 'voc.tar'. ```python url_voc = "http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar" voc, headers = urllib.request.urlretrieve(url_voc, 'voc.tar') ``` -------------------------------- ### Train Model with iqa_cnn_learner Source: https://github.com/baidut/paq-2-piq/wiki/Home Use `iqa_cnn_learner` when you have a model architecture (e.g., `models.resnet18`). This learner is a subclass of Fastai's `cnn_learner` with added functionalities for IQA tasks. Ensure you have the `Im2MOS` data object ready. ```python # iqa_cnn_learner is a sub class of cnn_learner in Fastai but with useful functins data = Im2MOS(CLIVE) model = models.resnet18 learn = iqa_cnn_learner(data, model) learn.fit(10) ``` -------------------------------- ### Download EMOTIC Dataset Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Downloads the EMOTIC dataset from a Google Drive link using a session to handle potential download warnings and confirmation tokens. Saves the file as 'emotic.zip'. ```python dest = os.path.dirname("database/voc_emotic_ava") session = requests.Session() response = session.get('https://docs.google.com/uc?export=download',params={'id': '0B7sjGeF4f3FYQUVlZ3ZOai1ieEU'}, stream=True) token = None for key, value in response.cookies.items(): if key.startswith('download_warning'): token = value if token: params = {'id': '0B7sjGeF4f3FYQUVlZ3ZOai1ieEU', 'confirm': token} response = session.get('https://docs.google.com/uc?export=download', params=params, stream=True) current_download_size = [0] with open('emotic.zip', 'wb') as f: for chunk in response.iter_content(32768): f.write(chunk) ``` -------------------------------- ### Load Data Bunch using NIMA Source: https://github.com/baidut/paq-2-piq/wiki/Home Utilize the NIMA model's bunching method to prepare the CLIVE database. This follows the data processing approach used by NIMA. ```python # use the way how NIMA processes the data to prepare CLIVE database data = NIMA.bunch(CLIVE) ``` -------------------------------- ### Move and Rename AVA Images Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Checks if the 'AVA_dataset' directory exists. If so, it renames all images within 'database/AVA_dataset/images/' to include an 'AVA__' prefix and moves them to 'database/voc_emotic_ava/'. Finally, it removes the original 'AVA_dataset' directory. ```python if os.path.isdir('database/AVA_dataset') == True: files = 'database/AVA_dataset/images/*' for path in glob.glob(files): name = path.split("/")[-1] newp = path.replace(name, "")+"AVA__"+name os.rename(path, newp) shutil.move(newp, 'database/voc_emotic_ava/') shutil.rmtree('database/voc_emotic_ava/AVA_dataset') ``` -------------------------------- ### Define Custom Data Bunch Source: https://github.com/baidut/paq-2-piq/wiki/Home Create a custom data bunch by inheriting from IqaDataBunch and implementing the get_data method to return a fastai data object. This allows for custom data loading and preparation logic. ```python class MyBunch(IqaDataBunch): def get_data(self): # write your code here # ... return data # return a fastai data object ``` -------------------------------- ### Define Subset of Custom IQA Database Source: https://github.com/baidut/paq-2-piq/wiki/Home This Python code defines a subset of a custom IQA database (PLIVE_256x256) by extending an existing label class (PLIVE). It overrides the 'folder' attribute to load images from a different directory. ```python class PLIVE_256x256(PLIVE): folder = '256x256' ``` -------------------------------- ### Inspect and Display Data Bunch Source: https://github.com/baidut/paq-2-piq/wiki/Home After creating a data bunch instance, you can inspect its information, such as the number of images in the train/validation split, and display a batch of data. ```python db= MyBunch(PLIVE) # check out information (e.g. # images in train/val split) print(db.data) # show a batch db.show_batch() ``` -------------------------------- ### Iterate and Sample Patches Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Iterates through image folders, reads patch information from a DataFrame, and uses the 'patch_sampling' function to extract and save specified patches. It checks if the patch name exists in the DataFrame before sampling. ```python for folder in folders: if folder != 'patches': im_paths = 'database/'+folder+"/*" for im_path in glob.glob(im_paths): im_name = im_path.split('/')[-1] patch_name = "patches/"+im_name row = df_coor.loc[df_coor['name_patch']==patch_name] if len(row) == 1: x1 = int(row['top_patch_1'].values.tolist()[0]) y1 = int(row['left_patch_1'].values.tolist()[0]) x2 = int(row['top_patch_2'].values.tolist()[0]) y2 = int(row['left_patch_2'].values.tolist()[0]) x3 = int(row['top_patch_3'].values.tolist()[0]) y3 = int(row['left_patch_3'].values.tolist()[0]) h1 = int(row['height_patch_1'].values.tolist()[0]) w1 = int(row['width_patch_1'].values.tolist()[0]) h2 = int(row['height_patch_2'].values.tolist()[0]) w2 = int(row['width_patch_2'].values.tolist()[0]) h3 = int(row['height_patch_3'].values.tolist()[0]) w3 = int(row['width_patch_3'].values.tolist()[0]) patch_sampling(im_path, 1, x1, y1, w1, h1) patch_sampling(im_path, 2, x2, y2, w2, h2) patch_sampling(im_path, 3, x3, y3, w3, h3) ``` -------------------------------- ### Extract and Organize EMOTIC Images Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Extracts the 'emotic.zip' file and renames image files within its subfolders to include an 'EMOTIC' prefix. Images are then moved to the main project directory and temporary directories are removed. ```python zipfile.ZipFile('emotic.zip', 'r').extractall('database/voc_emotic_ava') folders = os.listdir("database/voc_emotic_ava/emotic/") for folder in folders: files = "database/voc_emotic_ava/emotic/"+folder + "/images/*" for path in glob.glob(files): name = path.split("/")[-1] newp = path.replace(name,"")+"EMOTIC__"+name os.rename(path, newp) shutil.move(newp, 'database/voc_emotic_ava/') shutil.rmtree('database/voc_emotic_ava/emotic') shutil.rmtree('database/voc_emotic_ava/__MACOSX') ``` -------------------------------- ### Sample and Save Image Patch Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Defines a function to extract a rectangular patch from an image and save it to a specified path. The patch is identified by its index and coordinates. ```python def patch_sampling(im_path, patch_index, x, y, width, height): img=cv2.imread(im_path) patch = img[x:x+width, y:y+height] name = im_path.split('/')[-1] p_path = 'database/patches/'+name+"_"+"patch_"+str(patch_index)+".jpg" cv2.imwrite(p_path,patch) ``` -------------------------------- ### Python Version Check Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Ensures the script is running on Python 3. Raises an exception if an older version is detected. ```python if sys.version_info[0] < 3: raise Exception("Must be using Python 3") ``` -------------------------------- ### Define Custom IQA Database Label Class Source: https://github.com/baidut/paq-2-piq/wiki/Home This Python code defines a custom label class for a new IQA database (PLIVE), extending the base IqaLabel class. It specifies the path to data, label file, column names, and validation split percentage. ```python class PLIVE(IqaLabel): path = '!data/PLIVE' csv_labels = 'labels.csv' fn_col = 'name' label_cols = 'mos', # don't omit the comma here folder = 'images' valid_pct = 0.2 # how many percent of data for validation ``` -------------------------------- ### Clean Unused Images Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Iterates through all files in each subdirectory of 'database'. If a file's name (excluding the path) is not present in the 'ims' list (loaded from 'image_attri.csv'), the file is removed. ```python for folder in folders: files = 'database/'+folder+"/*" for path in glob.glob(files): name = path.replace("database/","") if name not in ims: os.remove(path) ``` -------------------------------- ### Import URL Retrieve Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Specific import for the urlretrieve function, used for downloading files from URLs. ```python from urllib.request import urlretrieve ``` -------------------------------- ### Process and Rename VOC Images Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Renames image files within the VOC dataset to include a 'VOC' prefix and moves them to the main directory. It also cleans up intermediate directories. ```python files = "database/voc_emotic_ava/VOCdevkit/VOC2012/JPEGImages/*" for path in glob.glob(files): name = path.split("/")[-1] newp1 = path.replace(name,"" )+"JPEGImages__"+name newp2 = path.replace(name, "")+"VOC"+name os.rename(path, newp1) shutil.copy(newp1, 'database/voc_emotic_ava/') os.rename(newp1, newp2) shutil.copy(newp2, 'database/voc_emotic_ava/') os.remove(newp2) shutil.rmtree('database/voc_emotic_ava/VOCdevkit') ``` -------------------------------- ### Load Patch Coordinates Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Reads patch coordinate data from a CSV file named 'all_patches.csv' into a pandas DataFrame. ```python df_coor = pd.read_csv('all_patches.csv') ``` -------------------------------- ### Extract VOC 2012 Dataset Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Extracts the contents of the downloaded 'voc.tar' file into the 'database/voc_emotic_ava' directory. ```python tar = tarfile.open('voc.tar') tar.extractall('database/voc_emotic_ava') ``` -------------------------------- ### Load Image Names from CSV Source: https://github.com/baidut/paq-2-piq/blob/master/database_prep.ipynb Reads image names from 'image_attri.csv' into a pandas DataFrame and extracts the 'name' column into a list. ```python df_ims = pd.read_csv('image_attri.csv') ims = df_ims['name'] ims = ims.values.tolist() ``` -------------------------------- ### Access Predicted Local Quality Scores Source: https://github.com/baidut/paq-2-piq/blob/master/demo.ipynb After predicting a quality map, you can access the underlying NumPy array containing the local quality scores using the '.mat' attribute. ```python qmap.mat ``` -------------------------------- ### Access Predicted Global Quality Score Source: https://github.com/baidut/paq-2-piq/blob/master/demo.ipynb The global quality score for the entire image can be accessed directly from the predicted quality map object using the '.global_score' attribute. ```python qmap.global_score ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.