Esempio n. 1
0
 def __init__(self, configfilename):
     ConfigProvider.__init__(self)
     if not os.path.isfile(configfilename):
         self.ConfigValid = False
         return
     self._config = configparser.ConfigParser()
     self._config.read(configfilename)
     self.ConfigValid = True
Esempio n. 2
0
def main():
    print("started")
    config = ConfigProvider.config()

    # read data
    inspected = cv2.imread(config.data.defective_inspected_path1, 0).astype('float32')
    reference = cv2.imread(config.data.defective_reference_path1, 0).astype('float32')

    # registration
    aligner = Aligner()
    warped, warp_mask = aligner.align_images(static=inspected, moving=reference)

    # find defects
    defect_segmenter = DefectSegmenter()
    defect_mask = defect_segmenter.segment_defects(inspected, warped, warp_mask)

    # observe results
    diff = np.zeros(inspected.shape, dtype=np.float32)
    diff[warp_mask] = (np.abs((np.float32(warped) - np.float32(inspected))))[warp_mask]
    noise_cleaner = NoiseCleaner()
    diff = noise_cleaner.clean_frame(diff, warp_mask)

    cv2.imshow("color_result", get_color_diff_image(inspected, defect_mask * 255).astype('uint8'))
    plt.imshow(diff.astype('uint8'), cmap='gray')
    plt.title("diff")
    cv2.imshow("inspected", inspected.astype('uint8'))
    cv2.imshow("reference", reference.astype('uint8'))
    cv2.imshow("result", defect_mask.astype('uint8') * 255)

    plt.show()
    cv2.waitKey(0)
Esempio n. 3
0
    def __init__(self):
        self._config = ConfigProvider.config()
        self._noise_cleaner = NoiseCleaner()

        self._min_diff_threshold = self._config.refinement.min_diff_threshold
        self._dilation_diameter = self._config.refinement.dilation_diameter
        self._min_new_connected_component_size = self._config.refinement.min_new_connected_component_size
    def __init__(self):
        self._config = ConfigProvider.config()
        self._noise_cleaner = NoiseCleaner()

        self._thread_defect_high_pass_thres = self._config.detection.thread_defect_high_pass_thres
        self._aura_radius = self._config.detection.aura_radius
        self._low_diff_far_from_edge_thres = self._config.detection.low_diff_far_from_edge_thres
        self._min_thread_defect_size = self._config.detection.min_thread_defect_size
    def __init__(self):
        self._config = ConfigProvider.config()
        self._is_force_translation = self._config.alignment.is_force_translation
        self._subpixel_accuracy_resolution = self._config.alignment.subpixel_accuracy_resolution

        self._detector = cv2.ORB_create()
        self._matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
        self._noise_cleaner = NoiseCleaner()
    def __init__(self):
        self._config = ConfigProvider.config()
        self._num_classes = self._config.segmentation.num_classes
        self._low_threshold = self._config.segmentation.low_threshold
        self._high_threshold = self._config.segmentation.high_threshold
        self._auto_thresholds = self._config.segmentation.auto_thresholds

        self._noise_cleaner = NoiseCleaner()
        self._kmeans = KMeans(n_clusters=self._num_classes)
class Plotter(object):
    config = ConfigProvider.config()
    is_plotting = config.misc.is_plotting

    @staticmethod
    def show_color_diff(im1, im2, title):
        if not Plotter.is_plotting:
            return
        to_show = Plotter.get_color_diff_image(im1, im2)
        figure()
        plt.title(title)
        imshow(to_show)

    @staticmethod
    def get_color_diff_image(im1, im2):
        to_show = np.ones((im1.shape[0], im1.shape[1], 3))
        to_show[:, :, 0] = im1
        to_show[:, :, 1] = im2
        to_show[:, :, 2] = im1
        to_show = to_show.astype('uint8')
        return to_show

    @staticmethod
    def show_color_diff_threeway(im1, im2, im3, title):
        to_show = np.ones((im1.shape[0], im1.shape[1], 3))
        to_show[:, :, 0] = im1
        to_show[:, :, 1] = im2
        to_show[:, :, 2] = im3
        to_show = to_show.astype('uint8')
        figure()
        plt.title(title)
        imshow(to_show)
        plt.show()
        return to_show

    @staticmethod
    def plot_image(im, title=""):
        if not Plotter.is_plotting:
            return
        figure()
        plt.title(title)
        imshow(im, cmap='gray')

    @staticmethod
    def plot_image_3d(im):
        if not Plotter.is_plotting:
            return
        xx, yy = np.mgrid[0:im.shape[0], 0:im.shape[1]]
        fig = plt.figure()
        ax = fig.gca(projection='3d')
        ax.plot_surface(xx,
                        yy,
                        im,
                        rstride=1,
                        cstride=1,
                        cmap=plt.cm.gray,
                        linewidth=0)
Esempio n. 8
0
    def __init__(self):
        self._config = ConfigProvider.config()
        self._refiner = DefectSegmentationRefiner()

        self._defect_segmenters = [
            DiffSegmenter(),
            BlurredDiffSegmenter(),
            LowDiffFarFromEdgeSegmenter(),
            ThreadDefectSegmenter()
        ]
Esempio n. 9
0
def main():
    print("started")
    config = ConfigProvider.config()

    # read data
    inspected = cv2.imread(config.data.defective_inspected_path1, 0).astype('float32')
    reference = cv2.imread(config.data.defective_reference_path1, 0).astype('float32')

    # registration
    aligner = Aligner()
    warped, warp_mask = aligner.align_images(static=inspected, moving=reference)

    # find defects
    segmenter = AutoencoderSegmenter()
    defect_mask = segmenter.segment_defects(inspected, warped, warp_mask)

    _observe_results(defect_mask, inspected, reference, warp_mask, warped)
 def __init__(self):
     ConfigProvider.__init__(self)
     self._config = configparser.ConfigParser()
     self.ConfigValid=True
Esempio n. 11
0
def dataset_single_image_default():
    path = ConfigProvider.config().data.defective_inspected_path1
    sample_shape = (11, 11)
    strides = (10, 10)
    dataset = DatasetSingleImage(path, sample_shape, strides)
    return dataset
 def __init__(self):
     self._config = ConfigProvider.config()
     self._median_blur_radius = self._config.noise_cleaning.median_blur_radius
     self._frame_radius = self._config.noise_cleaning.frame_radius
    def __init__(self):
        self._config = ConfigProvider.config()
        self._noise_cleaner = NoiseCleaner()

        self._aura_radius = self._config.detection.aura_radius
        self._low_diff_far_from_edge_thres = self._config.detection.low_diff_far_from_edge_thres
Esempio n. 14
0
    def __init__(self):
        self._config = ConfigProvider.config()
        self._noise_cleaner = NoiseCleaner()

        self._blured_diff_thres = self._config.detection.blured_diff_thres
Esempio n. 15
0
def train_autoencoder():
    print(
        f"running with device: {torch.cuda.get_device_name(torch.cuda.current_device())}"
    )

    seed = 42
    np.random.seed(seed)
    batch_size = 4
    num_workers = 0

    sample_shape = (11, 11)
    strides = (25, 25)

    is_plotting = False

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    bae = BasicAutoencoder(np.prod(sample_shape)).to(device)

    optimizer = optim.Adam(bae.parameters(), lr=1e-3)
    criterion = nn.MSELoss()

    path = ConfigProvider.config().data.defective_inspected_path1
    dataset = DatasetSingleImage(path, sample_shape, strides)

    train_size = int(len(dataset) * 0.7)
    test_size = len(dataset) - train_size
    train_dataset, test_dataset = \
        torch.utils.data.random_split(
            dataset,
            [train_size, test_size],
            generator=torch.Generator().manual_seed(seed))

    train_loader = DataLoader(
        train_dataset,
        batch_size=batch_size,
        shuffle=True,
        num_workers=num_workers,
    )
    test_loader = DataLoader(
        test_dataset,
        batch_size=batch_size,
        shuffle=False,
        num_workers=num_workers,
    )

    epochs = 200
    train_losses = np.zeros((epochs, ))
    test_losses = np.zeros((epochs, ))
    for epoch in range(epochs):
        bae.train()
        train_loss = 0
        for batch_features_train_ in train_loader:
            batch_features_train = batch_features_train_.float().view(
                -1, np.prod(sample_shape)).to(device)

            optimizer.zero_grad()

            outputs = bae(batch_features_train)
            train_loss = criterion(outputs, batch_features_train)

            train_loss.backward()
            optimizer.step()
            train_loss += train_loss.item()

        train_loss = train_loss / len(train_loader)
        train_losses[epoch] = train_loss
        print(f"train : {epoch + 1}/{epochs}, loss = {train_loss:.6f}")

        bae.eval()
        test_loss = 0
        batch_features_test_ = []
        outputs = []
        for batch_features_test_ in test_loader:
            batch_features_test = batch_features_test_.float().view(
                -1, np.prod(sample_shape)).to(device)

            outputs = bae(batch_features_test)
            test_loss = criterion(outputs, batch_features_test)

            test_loss.backward()
            test_loss += test_loss.item()

        test_loss = test_loss / len(test_loader)
        test_losses[epoch] = test_loss
        print(f"test : {epoch + 1}/{epochs}, loss = {test_loss:.6f}")

        if is_plotting and epoch % 50 == 0:
            num_samples = batch_features_test_.shape[0]
            fig, axs = plt.subplots(2, num_samples)
            for i_sample in range(num_samples):
                for i in range(2):
                    ax = axs[i, i_sample]
                    if i == 0:
                        ax.imshow(batch_features_test_[i_sample, :, :])
                    else:
                        ax.imshow((outputs.view(4, 10, 10, 3) /
                                   255).cpu().detach().numpy()[i_sample, :, :])
                    ax.axis("off")
                    ax.set_title(f"Sample #{i_sample}")
                    plt.pause(0.001)
            plt.close(fig)

    plt.figure()
    plt.plot(np.arange(epochs), train_losses, color='r', label='train loss')
    plt.plot(np.arange(epochs), test_losses, color='b', label='test loss')
    plt.legend(loc='upper right')
    plt.show()