Skip to content

Commit

Permalink
#132 Improve code quality by using ruff linter (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Only-bottle authored Jan 31, 2024
1 parent cace3df commit fc2e1e7
Show file tree
Hide file tree
Showing 53 changed files with 260 additions and 369 deletions.
3 changes: 1 addition & 2 deletions examples/compressor/automatic_compression.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"

Expand All @@ -12,7 +11,7 @@
# 2. Set variables for compression
INPUT_SHAPES = [{"batch": 1, "channel": 3, "dimension": [224, 224]}]
INPUT_MODEL_PATH = "./examples/sample_models/graphmodule.pt"
OUTPUT_DIR = "./outputs/compressed1/pytorch_automatic_compression_1"
OUTPUT_DIR = "./outputs/compressed/pytorch_automatic_compression_1"
COMPRESSION_RATIO = 0.5

# 3. Run automatic compression
Expand Down
1 change: 0 additions & 1 deletion examples/compressor/create_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from netspresso import NetsPresso
from netspresso.enums import Framework


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"

Expand Down
3 changes: 1 addition & 2 deletions examples/compressor/manual_compression.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso
from netspresso.enums import CompressionMethod, Policy, LayerNorm, GroupPolicy, Options

from netspresso.enums import CompressionMethod, GroupPolicy, LayerNorm, Options, Policy

EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"
Expand Down
1 change: 0 additions & 1 deletion examples/compressor/recommendation_compression.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from netspresso import NetsPresso
from netspresso.enums import CompressionMethod, RecommendationMethod


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"

Expand Down
3 changes: 1 addition & 2 deletions examples/converter & benchmarker/keras2tflite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName

from netspresso.enums import DeviceName, Framework

###
# Available target frameworks for conversion with keras models
Expand Down
3 changes: 1 addition & 2 deletions examples/converter & benchmarker/keras2tflite_int8.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName, DataType

from netspresso.enums import DataType, DeviceName, Framework

# Available target frameworks for conversion with keras models
#
Expand Down
3 changes: 1 addition & 2 deletions examples/converter & benchmarker/onnx2drpai.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName

from netspresso.enums import DeviceName, Framework

###
# Available target frameworks for conversion with onnx models
Expand Down
3 changes: 1 addition & 2 deletions examples/converter & benchmarker/onnx2openvino.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName

from netspresso.enums import DeviceName, Framework

###
# Available target frameworks for conversion with onnx models
Expand Down
7 changes: 4 additions & 3 deletions examples/converter & benchmarker/onnx2tensorrt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName, SoftwareVersion

from netspresso.enums import DeviceName, Framework, SoftwareVersion

###
# Available target frameworks for conversion with onnx models
Expand Down Expand Up @@ -68,7 +67,9 @@
benchmarker = netspresso.benchmarker()

# 5. Set variables for benchmark
CONVERTED_MODEL_PATH = "./outputs/converted/TENSORRT_JETSON_AGX_ORIN_JETPACK_5_0_1/TENSORRT_JETSON_AGX_ORIN_JETPACK_5_0_1.trt"
CONVERTED_MODEL_PATH = (
"./outputs/converted/TENSORRT_JETSON_AGX_ORIN_JETPACK_5_0_1/TENSORRT_JETSON_AGX_ORIN_JETPACK_5_0_1.trt"
)

# 6. Run benchmark
benchmark_task = benchmarker.benchmark_model(
Expand Down
3 changes: 1 addition & 2 deletions examples/converter & benchmarker/onnx2tflite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName

from netspresso.enums import DeviceName, Framework

###
# Available target frameworks for conversion with onnx models
Expand Down
3 changes: 1 addition & 2 deletions examples/converter & benchmarker/onnx2tflite_int8.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName, DataType

from netspresso.enums import DataType, DeviceName, Framework

###
# Available target frameworks for conversion with onnx models
Expand Down
3 changes: 1 addition & 2 deletions examples/trainer/example_with_config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from netspresso import NetsPresso
from netspresso.enums import Task
from netspresso.trainer.augmentations import Resize
from netspresso.trainer.optimizers import AdamW
from netspresso.trainer.schedulers import CosineAnnealingWarmRestartsWithCustomWarmUp
from netspresso.trainer.augmentations import Resize


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"
Expand Down
1 change: 0 additions & 1 deletion examples/trainer/example_with_config_for_retrain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from netspresso import NetsPresso
from netspresso.trainer.optimizers import AdamW


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"

Expand Down
1 change: 0 additions & 1 deletion netspresso/benchmarker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .benchmarker import Benchmarker


__all__ = ["Benchmarker"]
34 changes: 16 additions & 18 deletions netspresso/benchmarker/benchmarker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@

from loguru import logger

from netspresso.clients.auth import auth_client, TokenHandler
from netspresso.clients.auth import TokenHandler, auth_client
from netspresso.clients.auth.schemas.auth import UserInfo
from netspresso.clients.launcher import launcher_client
from netspresso.clients.launcher.schemas import TargetDeviceFilter, BenchmarkTask
from netspresso.enums import (
DataType,
DeviceName,
HardwareType,
Module,
ServiceCredit,
SoftwareVersion,
Status,
TaskStatus,
TaskType,
)
from netspresso.clients.launcher.schemas import BenchmarkTask, TargetDeviceFilter
from netspresso.enums.credit import ServiceCredit
from netspresso.enums.device import DeviceName, HardwareType, SoftwareVersion, TaskStatus
from netspresso.enums.metadata import Status, TaskType
from netspresso.enums.model import DataType
from netspresso.enums.module import Module

from ..utils import FileHandler, check_credit_balance
from ..utils.metadata import MetadataHandler
Expand Down Expand Up @@ -66,8 +60,8 @@ def benchmark_model(
folder_path = Path(input_model_path).parent

metadata = MetadataHandler.get_default_metadata(TaskType.BENCHMARK)
if FileHandler.check_exists(folder_path / f"benchmark.json"):
metadatas = MetadataHandler.load_json(folder_path / f"benchmark.json")
if FileHandler.check_exists(folder_path / "benchmark.json"):
metadatas = MetadataHandler.load_json(folder_path / "benchmark.json")
metadatas.append(metadata.asdict())
else:
metadatas = [metadata.asdict()]
Expand All @@ -76,7 +70,9 @@ def benchmark_model(
current_credit = auth_client.get_credit(self.token_handler.tokens.access_token)
check_credit_balance(user_credit=current_credit, service_credit=ServiceCredit.MODEL_BENCHMARK)
model = launcher_client.upload_model(
model_file_path=input_model_path, target_function=Module.BENCHMARK, access_token=self.token_handler.tokens.access_token
model_file_path=input_model_path,
target_function=Module.BENCHMARK,
access_token=self.token_handler.tokens.access_token,
)
model_uuid = model.model_uuid

Expand Down Expand Up @@ -201,7 +197,7 @@ def get_benchmark_task(self, benchmark_task: Union[str, BenchmarkTask]) -> Bench

try:
task_uuid = None
if type(benchmark_task) is str:
if isinstance(benchmark_task, str):
task_uuid = benchmark_task
elif type(benchmark_task) is BenchmarkTask:
task_uuid = benchmark_task.benchmark_task_uuid
Expand All @@ -210,7 +206,9 @@ def get_benchmark_task(self, benchmark_task: Union[str, BenchmarkTask]) -> Bench
"There is no available function for the given parameter. The 'benchmark_task' should be a UUID string or a ModelBenchmark object."
)

return launcher_client.get_benchmark(benchmark_task_uuid=task_uuid, access_token=self.token_handler.tokens.access_token)
return launcher_client.get_benchmark(
benchmark_task_uuid=task_uuid, access_token=self.token_handler.tokens.access_token
)

except Exception as e:
logger.error(f"Get benchmark failed. Error: {e}")
2 changes: 1 addition & 1 deletion netspresso/clients/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .main import auth_client, TokenHandler
from .main import TokenHandler, auth_client

__all__ = ["auth_client", "TokenHandler"]
25 changes: 7 additions & 18 deletions netspresso/clients/auth/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import json
from datetime import datetime

import requests
from loguru import logger
import jwt
import pytz
import requests
from loguru import logger

from netspresso.clients.auth.schemas import (
LoginRequest,
LoginResponse,
Tokens,
UserInfo,
)
from netspresso.clients.auth.schemas import LoginRequest, LoginResponse, Tokens, UserInfo
from netspresso.clients.config import Config, Module
from netspresso.clients.utils import get_headers

Expand Down Expand Up @@ -52,9 +47,7 @@ def login(self, email, password) -> Tokens:
def get_user_info(self, access_token) -> UserInfo:
try:
url = f"{self.base_url}/user"
response = requests.get(
url, headers=get_headers(access_token=access_token)
)
response = requests.get(url, headers=get_headers(access_token=access_token))
response_body = json.loads(response.text)

if response.status_code == 200 or response.status_code == 201:
Expand All @@ -70,18 +63,14 @@ def get_user_info(self, access_token) -> UserInfo:

def get_credit(self, access_token) -> int:
user_info = self.get_user_info(access_token)

return user_info.total

def reissue_token(self, access_token, refresh_token) -> Tokens:
try:
url = f"{self.base_url}/auth/token"
data = Tokens(
access_token=access_token, refresh_token=refresh_token
)
response = requests.post(
url, data=data.json(), headers=get_headers(json_type=True)
)
data = Tokens(access_token=access_token, refresh_token=refresh_token)
response = requests.post(url, data=data.json(), headers=get_headers(json_type=True))
response_body = json.loads(response.text)

if response.status_code == 200 or response.status_code == 201:
Expand Down
34 changes: 8 additions & 26 deletions netspresso/clients/compressor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
GetAvailableLayersReponse,
RecommendationResponse,
)
from netspresso.clients.compressor.schemas.model import (
GetDownloadLinkResponse,
ModelResponse,
UploadModelRequest,
)
from netspresso.clients.compressor.schemas.model import GetDownloadLinkResponse, ModelResponse, UploadModelRequest
from netspresso.clients.config import Config, Module
from netspresso.clients.utils.common import get_files, get_headers

Expand All @@ -27,9 +23,7 @@ def __init__(self):
def upload_model(self, data: UploadModelRequest, access_token) -> ModelResponse:
url = f"{self.url}/models"
files = get_files(data.file_path)
response = requests.post(
url, data=data.dict(), files=files, headers=get_headers(access_token)
)
response = requests.post(url, data=data.dict(), files=files, headers=get_headers(access_token))
response_body = json.loads(response.text)

if response.status_code == 200:
Expand Down Expand Up @@ -67,9 +61,7 @@ def get_model_info(self, model_id, access_token) -> ModelResponse:
else:
raise Exception(response_body["detail"])

def get_download_model_link(
self, model_id, access_token
) -> GetDownloadLinkResponse:
def get_download_model_link(self, model_id, access_token) -> GetDownloadLinkResponse:
url = f"{self.url}/models/{model_id}/download"
response = requests.post(url, headers=get_headers(access_token))
response_body = json.loads(response.text)
Expand All @@ -91,9 +83,7 @@ def delete_model(self, model_id, access_token):

def get_available_layers(self, data, access_token) -> GetAvailableLayersReponse:
url = f"{self.url}/models/{data.model_id}/get_available_layers"
response = requests.post(
url, data=data.json(), headers=get_headers(access_token, json_type=True)
)
response = requests.post(url, data=data.json(), headers=get_headers(access_token, json_type=True))
response_body = json.loads(response.text)

if response.status_code == 200:
Expand All @@ -103,9 +93,7 @@ def get_available_layers(self, data, access_token) -> GetAvailableLayersReponse:

def create_compression(self, data, access_token) -> CompressionResponse:
url = f"{self.url}/compressions"
response = requests.post(
url, data=data.json(), headers=get_headers(access_token, json_type=True)
)
response = requests.post(url, data=data.json(), headers=get_headers(access_token, json_type=True))
response_body = json.loads(response.text)

if response.status_code == 200:
Expand All @@ -115,9 +103,7 @@ def create_compression(self, data, access_token) -> CompressionResponse:

def get_recommendation(self, data, access_token) -> RecommendationResponse:
url = f"{self.url}/models/{data.model_id}/recommendation"
response = requests.post(
url, data=data.json(), headers=get_headers(access_token, json_type=True)
)
response = requests.post(url, data=data.json(), headers=get_headers(access_token, json_type=True))
response_body = json.loads(response.text)

if response.status_code == 200:
Expand All @@ -128,9 +114,7 @@ def get_recommendation(self, data, access_token) -> RecommendationResponse:

def compress_model(self, data, access_token):
url = f"{self.url}/compressions/{data.compression_id}"
response = requests.put(
url, data=data.json(), headers=get_headers(access_token, json_type=True)
)
response = requests.put(url, data=data.json(), headers=get_headers(access_token, json_type=True))
response_body = json.loads(response.text)

if response.status_code == 200:
Expand All @@ -140,9 +124,7 @@ def compress_model(self, data, access_token):

def auto_compression(self, data, access_token):
url = f"{self.url}/models/{data.model_id}/auto_compress"
response = requests.post(
url, data=data.json(), headers=get_headers(access_token, json_type=True)
)
response = requests.post(url, data=data.json(), headers=get_headers(access_token, json_type=True))
response_body = json.loads(response.text)

if response.status_code == 200:
Expand Down
Loading

0 comments on commit fc2e1e7

Please sign in to comment.