-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add oneflow_pytorch_compatiblity_test * add compatiblity test * align init model * add alexnet * fix comments * restruct code structure * add resnet50 and restruct structure * Delete loss_compare.png * fix comment * make dataset and modelzoo read only * fix comments * refine * fix bug * fix comments * auto format by CI * fix ci error Co-authored-by: tsai <jackalcooper@gmail.com> Co-authored-by: oneflow-ci-bot <69100618+oneflow-ci-bot@users.noreply.github.com> Co-authored-by: oneflow-ci-bot <ci-bot@oneflow.org>
- Loading branch information
1 parent
0cf55eb
commit 01c55a0
Showing
7 changed files
with
903 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
Copyright 2020 The OneFlow Authors. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
import os | ||
import importlib.machinery | ||
|
||
|
||
def _download_file_from_remote_location(fpath: str, url: str) -> None: | ||
pass | ||
|
||
|
||
def _is_remote_location_available() -> bool: | ||
return False | ||
|
||
|
||
try: | ||
from torch.hub import load_state_dict_from_url | ||
except ImportError: | ||
from torch.utils.model_zoo import load_url as load_state_dict_from_url | ||
|
||
|
||
def _get_extension_path(lib_name): | ||
|
||
lib_dir = os.path.dirname(__file__) | ||
if os.name == "nt": | ||
# Register the main torchvision library location on the default DLL path | ||
import ctypes | ||
import sys | ||
|
||
kernel32 = ctypes.WinDLL("kernel32.dll", use_last_error=True) | ||
with_load_library_flags = hasattr(kernel32, "AddDllDirectory") | ||
prev_error_mode = kernel32.SetErrorMode(0x0001) | ||
|
||
if with_load_library_flags: | ||
kernel32.AddDllDirectory.restype = ctypes.c_void_p | ||
|
||
if sys.version_info >= (3, 8): | ||
os.add_dll_directory(lib_dir) | ||
elif with_load_library_flags: | ||
res = kernel32.AddDllDirectory(lib_dir) | ||
if res is None: | ||
err = ctypes.WinError(ctypes.get_last_error()) | ||
err.strerror += f' Error adding "{lib_dir}" to the DLL directories.' | ||
raise err | ||
|
||
kernel32.SetErrorMode(prev_error_mode) | ||
|
||
loader_details = ( | ||
importlib.machinery.ExtensionFileLoader, | ||
importlib.machinery.EXTENSION_SUFFIXES, | ||
) | ||
|
||
extfinder = importlib.machinery.FileFinder(lib_dir, loader_details) | ||
ext_specs = extfinder.find_spec(lib_name) | ||
if ext_specs is None: | ||
raise ImportError | ||
|
||
return ext_specs.origin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Copyright 2020 The OneFlow Authors. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
from _internally_replaced_utils import load_state_dict_from_url | ||
from typing import Any | ||
|
||
|
||
__all__ = ["AlexNet", "alexnet"] | ||
|
||
|
||
model_urls = { | ||
"alexnet": "https://download.pytorch.org/models/alexnet-owt-7be5be79.pth", | ||
} | ||
|
||
|
||
class AlexNet(nn.Module): | ||
def __init__(self, num_classes: int = 1000) -> None: | ||
super(AlexNet, self).__init__() | ||
self.features = nn.Sequential( | ||
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2), | ||
nn.ReLU(inplace=True), | ||
nn.MaxPool2d(kernel_size=3, stride=2), | ||
nn.Conv2d(64, 192, kernel_size=5, padding=2), | ||
nn.ReLU(inplace=True), | ||
nn.MaxPool2d(kernel_size=3, stride=2), | ||
nn.Conv2d(192, 384, kernel_size=3, padding=1), | ||
nn.ReLU(inplace=True), | ||
nn.Conv2d(384, 256, kernel_size=3, padding=1), | ||
nn.ReLU(inplace=True), | ||
nn.Conv2d(256, 256, kernel_size=3, padding=1), | ||
nn.ReLU(inplace=True), | ||
nn.MaxPool2d(kernel_size=3, stride=2), | ||
) | ||
self.avgpool = nn.AdaptiveAvgPool2d((6, 6)) | ||
self.classifier = nn.Sequential( | ||
nn.Dropout(), | ||
nn.Linear(256 * 6 * 6, 4096), | ||
nn.ReLU(inplace=True), | ||
nn.Dropout(), | ||
nn.Linear(4096, 4096), | ||
nn.ReLU(inplace=True), | ||
nn.Linear(4096, num_classes), | ||
) | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
x = self.features(x) | ||
x = self.avgpool(x) | ||
x = torch.flatten(x, 1) | ||
x = self.classifier(x) | ||
return x | ||
|
||
|
||
def alexnet(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> AlexNet: | ||
r"""AlexNet model architecture from the | ||
`"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper. | ||
The required minimum input size of the model is 63x63. | ||
Args: | ||
pretrained (bool): If True, returns a model pre-trained on ImageNet | ||
progress (bool): If True, displays a progress bar of the download to stderr | ||
""" | ||
model = AlexNet(**kwargs) | ||
if pretrained: | ||
state_dict = load_state_dict_from_url(model_urls["alexnet"], progress=progress) | ||
model.load_state_dict(state_dict) | ||
return model |
Oops, something went wrong.